1 /*
2  * Copyright 2017 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 "ok.h"
9 #include "SkPictureRecorder.h"
10 
proxy(Src * original,std::function<bool (SkCanvas *)> fn)11 static std::unique_ptr<Src> proxy(Src* original, std::function<bool(SkCanvas*)> fn) {
12     struct : Src {
13         Src*                           original;
14         std::function<bool(SkCanvas*)> fn;
15 
16         std::string name() override { return original->name(); }
17         SkISize     size() override { return original->size(); }
18         bool draw(SkCanvas* canvas) override { return fn(canvas); }
19     } src;
20     src.original = original;
21     src.fn       = fn;
22     return move_unique(src);
23 }
24 
25 struct ViaPic : Dst {
26     std::unique_ptr<Dst> target;
27     bool                 rtree = false;
28 
CreateViaPic29     static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
30         ViaPic via;
31         via.target = std::move(dst);
32         if (options("bbh") == "rtree") { via.rtree = true; }
33         return move_unique(via);
34     }
35 
drawViaPic36     bool draw(Src* src) override {
37         SkRTreeFactory factory;
38         SkPictureRecorder rec;
39         rec.beginRecording(SkRect::MakeSize(SkSize::Make(src->size())),
40                            rtree ? &factory : nullptr);
41 
42         if (!src->draw(rec.getRecordingCanvas())) {
43             return false;
44         }
45         auto pic = rec.finishRecordingAsPicture();
46 
47         return target->draw(proxy(src, [=](SkCanvas* canvas) {
48             pic->playback(canvas);
49             return true;
50         }).get());
51     }
52 
imageViaPic53     sk_sp<SkImage> image() override {
54         return target->image();
55     }
56 };
57 static Register via_pic{"via_pic", ViaPic::Create};
58