• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tests/Test.h"
9 
10 #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"
16 #include "src/core/SkBlendModePriv.h"
17 
18 #include <cstring>
19 
20 // Verify that replay of a recording into a clipped canvas
21 // produces the correct bitmap.
22 // This arose from http://crbug.com/401593 which has
23 // https://code.google.com/p/skia/issues/detail?id=1291 as its root cause.
24 
25 namespace {
26 
27 class Drawer {
28  public:
Drawer()29     explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
30         fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
31         SkCanvas canvas(fCircleBM);
32         canvas.clear(0xffffffff);
33         SkPaint circlePaint;
34         circlePaint.setColor(0xff000000);
35         canvas.drawCircle(50, 50, 50, circlePaint);
36     }
37 
imageInfo() const38     const SkImageInfo& imageInfo() const { return fImageInfo; }
39 
draw(SkCanvas * canvas,const SkRect & clipRect,SkBlendMode mode) const40     void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
41         SkPaint greenPaint;
42         greenPaint.setColor(0xff008000);
43         SkPaint blackPaint;
44         blackPaint.setColor(0xff000000);
45         SkPaint whitePaint;
46         whitePaint.setColor(0xffffffff);
47         SkPaint layerPaint;
48         layerPaint.setColor(0xff000000);
49         layerPaint.setBlendMode(mode);
50         SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
51                                          SkIntToScalar(fImageInfo.height())));
52 
53         canvas->clipRect(clipRect);
54         canvas->clear(0xff000000);
55 
56         canvas->saveLayer(nullptr, &blackPaint);
57             canvas->drawRect(canvasRect, greenPaint);
58             canvas->saveLayer(nullptr, &layerPaint);
59                 canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
60             canvas->restore();
61         canvas->restore();
62     }
63 
64  private:
65     const SkImageInfo fImageInfo;
66     SkBitmap fCircleBM;
67 };
68 
69 class RecordingStrategy {
70  public:
~RecordingStrategy()71     virtual ~RecordingStrategy() {}
72     virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
73                                             const SkRect& intoClip,
74                                             SkBlendMode) = 0;
75 };
76 
77 class BitmapBackedCanvasStrategy : public RecordingStrategy {
78     // This version just draws into a bitmap-backed canvas.
79  public:
BitmapBackedCanvasStrategy(const SkImageInfo & imageInfo)80     BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
81         fBitmap.allocPixels(imageInfo);
82     }
83 
recordAndReplay(const Drawer & drawer,const SkRect & intoClip,SkBlendMode mode)84     const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
85                                     SkBlendMode mode) override {
86         SkCanvas canvas(fBitmap);
87         canvas.clear(0xffffffff);
88         // Note that the scene is drawn just into the clipped region!
89         canvas.clipRect(intoClip);
90         drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide...
91         return fBitmap;
92     }
93 
94  private:
95     SkBitmap fBitmap;
96 };
97 
98 class PictureStrategy : public RecordingStrategy {
99     // This version draws the entire scene into an SkPictureRecorder.
100     // Then it then replays the scene through a clip rectangle.
101     // This backend proved to be buggy.
102  public:
PictureStrategy(const SkImageInfo & imageInfo)103     PictureStrategy(const SkImageInfo& imageInfo) {
104         fBitmap.allocPixels(imageInfo);
105         fWidth  = imageInfo.width();
106         fHeight = imageInfo.height();
107     }
108 
recordAndReplay(const Drawer & drawer,const SkRect & intoClip,SkBlendMode mode)109     const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
110                                     SkBlendMode mode) override {
111         SkRTreeFactory factory;
112         SkPictureRecorder recorder;
113         SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
114         SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
115                                                    SkIntToScalar(fHeight),
116                                                    &factory);
117         drawer.draw(canvas, canvasRect, mode);
118         sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
119 
120         SkCanvas replayCanvas(fBitmap);
121         replayCanvas.clear(0xffffffff);
122         replayCanvas.clipRect(intoClip);
123         picture->playback(&replayCanvas);
124         return fBitmap;
125     }
126 
127  private:
128     SkBitmap fBitmap;
129     int fWidth;
130     int fHeight;
131 };
132 
133 } // namespace
134 
135 
DEF_TEST(SkRecordingAccuracyXfermode,reporter)136 DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
137 #define FINEGRAIN 0
138     const Drawer drawer;
139 
140     BitmapBackedCanvasStrategy golden(drawer.imageInfo());
141     PictureStrategy picture(drawer.imageInfo());
142 
143 #if !FINEGRAIN
144     unsigned numErrors = 0;
145     SkString errors;
146 #endif
147 
148     for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
149         const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
150         SkBlendMode mode = SkBlendMode(iMode);
151 
152         const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
153         const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
154 
155         size_t pixelsSize = goldenBM.computeByteSize();
156         REPORTER_ASSERT(reporter, pixelsSize == pictureBM.computeByteSize());
157 
158         // The pixel arrays should match.
159 #if FINEGRAIN
160         REPORTER_ASSERT(reporter,
161                         0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
162 #else
163         if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
164             numErrors++;
165             errors.appendf("For SkXfermode %d %s:    SkPictureRecorder bitmap is wrong\n",
166                            iMode, SkBlendMode_Name(mode));
167         }
168 #endif
169     }
170 
171 #if !FINEGRAIN
172     REPORTER_ASSERT(reporter, 0 == numErrors, errors.c_str());
173 #endif
174 }
175