2014-09-15 17:53:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
2017-05-16 14:39:06 +00:00
|
|
|
#include "../include/core/SkBitmap.h"
|
2014-09-15 17:53:16 +00:00
|
|
|
#include "../include/core/SkCanvas.h"
|
|
|
|
#include "../include/core/SkPicture.h"
|
|
|
|
#include "../include/core/SkStream.h"
|
|
|
|
#include "../include/core/SkString.h"
|
|
|
|
#include "../include/core/SkPictureRecorder.h"
|
2016-11-15 21:44:34 +00:00
|
|
|
#include "../src/core/SkBlendModePriv.h"
|
2014-09-15 17:53:16 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
// Verify that replay of a recording into a clipped canvas
|
|
|
|
// produces the correct bitmap.
|
|
|
|
// This arose from http://crbug.com/401593 which has
|
|
|
|
// https://code.google.com/p/skia/issues/detail?id=1291 as its root cause.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Drawer {
|
|
|
|
public:
|
2014-09-30 21:47:10 +00:00
|
|
|
explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
|
|
|
|
fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
|
2014-09-15 17:53:16 +00:00
|
|
|
SkCanvas canvas(fCircleBM);
|
|
|
|
canvas.clear(0xffffffff);
|
|
|
|
SkPaint circlePaint;
|
|
|
|
circlePaint.setColor(0xff000000);
|
2014-09-30 21:47:10 +00:00
|
|
|
canvas.drawCircle(50, 50, 50, circlePaint);
|
2014-09-15 17:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SkImageInfo& imageInfo() const { return fImageInfo; }
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
|
2014-09-15 17:53:16 +00:00
|
|
|
SkPaint greenPaint;
|
|
|
|
greenPaint.setColor(0xff008000);
|
|
|
|
SkPaint blackPaint;
|
|
|
|
blackPaint.setColor(0xff000000);
|
|
|
|
SkPaint whitePaint;
|
|
|
|
whitePaint.setColor(0xffffffff);
|
|
|
|
SkPaint layerPaint;
|
|
|
|
layerPaint.setColor(0xff000000);
|
2016-10-06 00:33:02 +00:00
|
|
|
layerPaint.setBlendMode(mode);
|
2014-09-30 21:47:10 +00:00
|
|
|
SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
|
|
|
|
SkIntToScalar(fImageInfo.height())));
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
canvas->clipRect(clipRect);
|
|
|
|
canvas->clear(0xff000000);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &blackPaint);
|
2014-09-30 21:47:10 +00:00
|
|
|
canvas->drawRect(canvasRect, greenPaint);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &layerPaint);
|
2014-09-30 21:47:10 +00:00
|
|
|
canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
|
2014-09-15 17:53:16 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkImageInfo fImageInfo;
|
|
|
|
SkBitmap fCircleBM;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RecordingStrategy {
|
|
|
|
public:
|
|
|
|
virtual ~RecordingStrategy() {}
|
|
|
|
virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
|
|
|
|
const SkRect& intoClip,
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode) = 0;
|
2014-09-15 17:53:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BitmapBackedCanvasStrategy : public RecordingStrategy {
|
|
|
|
// This version just draws into a bitmap-backed canvas.
|
|
|
|
public:
|
2014-09-30 21:47:10 +00:00
|
|
|
BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
|
2014-09-15 17:53:16 +00:00
|
|
|
fBitmap.allocPixels(imageInfo);
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
|
|
|
|
SkBlendMode mode) override {
|
2014-09-15 17:53:16 +00:00
|
|
|
SkCanvas canvas(fBitmap);
|
|
|
|
canvas.clear(0xffffffff);
|
|
|
|
// Note that the scene is drawn just into the clipped region!
|
|
|
|
canvas.clipRect(intoClip);
|
|
|
|
drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide...
|
|
|
|
return fBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
};
|
|
|
|
|
2014-09-30 21:47:10 +00:00
|
|
|
class PictureStrategy : public RecordingStrategy {
|
|
|
|
// This version draws the entire scene into an SkPictureRecorder.
|
2014-09-15 17:53:16 +00:00
|
|
|
// Then it then replays the scene through a clip rectangle.
|
|
|
|
// This backend proved to be buggy.
|
|
|
|
public:
|
2014-09-30 21:47:10 +00:00
|
|
|
PictureStrategy(const SkImageInfo& imageInfo) {
|
2014-09-15 17:53:16 +00:00
|
|
|
fBitmap.allocPixels(imageInfo);
|
2014-09-30 21:47:10 +00:00
|
|
|
fWidth = imageInfo.width();
|
|
|
|
fHeight = imageInfo.height();
|
2014-09-15 17:53:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
|
|
|
|
SkBlendMode mode) override {
|
2015-01-09 14:41:48 +00:00
|
|
|
SkRTreeFactory factory;
|
2014-09-15 17:53:16 +00:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
|
2014-09-30 21:47:10 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
|
|
|
|
SkIntToScalar(fHeight),
|
|
|
|
&factory);
|
2014-09-15 17:53:16 +00:00
|
|
|
drawer.draw(canvas, canvasRect, mode);
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
SkCanvas replayCanvas(fBitmap);
|
|
|
|
replayCanvas.clear(0xffffffff);
|
|
|
|
replayCanvas.clipRect(intoClip);
|
|
|
|
picture->playback(&replayCanvas);
|
|
|
|
return fBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
int fWidth;
|
|
|
|
int fHeight;
|
|
|
|
};
|
|
|
|
|
2014-09-30 21:47:10 +00:00
|
|
|
} // namespace
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
|
|
|
|
#define FINEGRAIN 0
|
|
|
|
const Drawer drawer;
|
|
|
|
|
2014-09-30 21:47:10 +00:00
|
|
|
BitmapBackedCanvasStrategy golden(drawer.imageInfo());
|
|
|
|
PictureStrategy picture(drawer.imageInfo());
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
#if !FINEGRAIN
|
|
|
|
unsigned numErrors = 0;
|
|
|
|
SkString errors;
|
|
|
|
#endif
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
|
2014-09-30 21:47:10 +00:00
|
|
|
const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode mode = SkBlendMode(iMode);
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
|
2014-09-30 21:47:10 +00:00
|
|
|
const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
size_t pixelsSize = goldenBM.getSize();
|
2014-09-30 21:47:10 +00:00
|
|
|
REPORTER_ASSERT(reporter, pixelsSize == pictureBM.getSize());
|
2014-09-15 17:53:16 +00:00
|
|
|
|
|
|
|
// The pixel arrays should match.
|
|
|
|
#if FINEGRAIN
|
2014-09-30 21:47:10 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
|
2014-09-15 17:53:16 +00:00
|
|
|
#else
|
2014-09-30 21:47:10 +00:00
|
|
|
if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
|
2014-09-15 17:53:16 +00:00
|
|
|
numErrors++;
|
2014-09-30 21:47:10 +00:00
|
|
|
errors.appendf("For SkXfermode %d %s: SkPictureRecorder bitmap is wrong\n",
|
2016-11-15 21:44:34 +00:00
|
|
|
iMode, SkBlendMode_Name(mode));
|
2014-09-15 17:53:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2014-09-30 21:47:10 +00:00
|
|
|
|
2014-09-15 17:53:16 +00:00
|
|
|
#if !FINEGRAIN
|
2014-09-30 21:47:10 +00:00
|
|
|
REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str());
|
2014-09-15 17:53:16 +00:00
|
|
|
#endif
|
|
|
|
}
|