Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / dm / DMSKPTask.cpp
1 #include "DMSKPTask.h"
2 #include "DMUtil.h"
3 #include "DMWriteTask.h"
4
5 #include "SkCommandLineFlags.h"
6 #include "SkPictureRecorder.h"
7
8 DEFINE_bool(skr, true, "Test that SKPs draw the same when re-recorded with SkRecord backend.");
9 DEFINE_int32(skpMaxWidth,  1000, "Max SKPTask viewport width.");
10 DEFINE_int32(skpMaxHeight, 1000, "Max SKPTask viewport height.");
11
12 namespace DM {
13
14 // Test that an SkPicture plays back the same when re-recorded into an
15 // SkPicture backed by SkRecord.
16 class SkrComparisonTask : public CpuTask {
17 public:
18     SkrComparisonTask(const Task& parent, const SkPicture* picture, SkBitmap reference)
19         : CpuTask(parent)
20         , fPicture(SkRef(picture))
21         , fReference(reference)
22         , fName(UnderJoin(parent.name().c_str(), "skr")) {}
23
24     virtual bool shouldSkip() const SK_OVERRIDE { return !FLAGS_skr; }
25     virtual SkString name() const SK_OVERRIDE { return fName; }
26
27     virtual void draw() SK_OVERRIDE {
28         SkPictureRecorder recorder;
29         fPicture->draw(recorder.EXPERIMENTAL_beginRecording(fPicture->width(), fPicture->height()));
30         SkAutoTDelete<const SkPicture> skrPicture(recorder.endRecording());
31
32         SkBitmap bitmap;
33         AllocatePixels(kN32_SkColorType, fReference.width(), fReference.height(), &bitmap);
34         DrawPicture(*skrPicture, &bitmap);
35
36         if (!BitmapsEqual(fReference, bitmap)) {
37             this->fail();
38             this->spawnChild(SkNEW_ARGS(WriteTask, (*this, bitmap)));
39         }
40     }
41
42 private:
43     SkAutoTUnref<const SkPicture> fPicture;
44     const SkBitmap fReference;
45     const SkString fName;
46 };
47
48
49 SKPTask::SKPTask(Reporter* r, TaskRunner* tr, const SkPicture* pic, SkString filename)
50     : CpuTask(r, tr), fPicture(SkRef(pic)), fName(FileToTaskName(filename)) {}
51
52 void SKPTask::draw() {
53     const int width  = SkTMin(fPicture->width(),  FLAGS_skpMaxWidth),
54               height = SkTMin(fPicture->height(), FLAGS_skpMaxHeight);
55     SkBitmap bitmap;
56     AllocatePixels(kN32_SkColorType, width, height, &bitmap);
57     DrawPicture(*fPicture, &bitmap);
58
59     this->spawnChild(SkNEW_ARGS(WriteTask, (*this, bitmap)));
60     this->spawnChild(SkNEW_ARGS(SkrComparisonTask, (*this, fPicture.get(), bitmap)));
61 }
62
63 }  // namespace DM