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 "include/core/SkCanvas.h"
9#include "include/core/SkDrawable.h"
10#include <atomic>
11
12static int32_t next_generation_id() {
13 static std::atomic<int32_t> nextID{1};
14
15 int32_t id;
16 do {
17 id = nextID++;
18 } while (id == 0);
19 return id;
20}
21
22SkDrawable::SkDrawable() : fGenerationID(0) {}
23
24static void draw_bbox(SkCanvas* canvas, const SkRect& r) {
25 SkPaint paint;
26 paint.setStyle(SkPaint::kStroke_Style);
27 paint.setColor(0xFFFF7088);
28 canvas->drawRect(r, paint);
29 canvas->drawLine(r.left(), r.top(), r.right(), r.bottom(), paint);
30 canvas->drawLine(r.left(), r.bottom(), r.right(), r.top(), paint);
31}
32
33void SkDrawable::draw(SkCanvas* canvas, const SkMatrix* matrix) {
34 SkAutoCanvasRestore acr(canvas, true);
35 if (matrix) {
36 canvas->concat(*matrix);
37 }
38 this->onDraw(canvas);
39
40 if (false) {
41 draw_bbox(canvas, this->getBounds());
42 }
43}
44
45void SkDrawable::draw(SkCanvas* canvas, SkScalar x, SkScalar y) {
46 SkMatrix matrix = SkMatrix::MakeTrans(x, y);
47 this->draw(canvas, &matrix);
48}
49
50SkPicture* SkDrawable::newPictureSnapshot() {
51 return this->onNewPictureSnapshot();
52}
53
54uint32_t SkDrawable::getGenerationID() {
55 if (0 == fGenerationID) {
56 fGenerationID = next_generation_id();
57 }
58 return fGenerationID;
59}
60
61SkRect SkDrawable::getBounds() {
62 return this->onGetBounds();
63}
64
65void SkDrawable::notifyDrawingChanged() {
66 fGenerationID = 0;
67}
68
69/////////////////////////////////////////////////////////////////////////////////////////
70
71#include "include/core/SkPictureRecorder.h"
72
73SkPicture* SkDrawable::onNewPictureSnapshot() {
74 SkPictureRecorder recorder;
75
76 const SkRect bounds = this->getBounds();
77 SkCanvas* canvas = recorder.beginRecording(bounds, nullptr, 0);
78 this->draw(canvas);
79 if (false) {
80 draw_bbox(canvas, bounds);
81 }
82 return recorder.finishRecordingAsPicture().release();
83}
84