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
next_generation_id()12 static int32_t next_generation_id() {
13 static std::atomic<int32_t> nextID{1};
14
15 int32_t id;
16 do {
17 id = nextID.fetch_add(1, std::memory_order_relaxed);
18 } while (id == 0);
19 return id;
20 }
21
SkDrawable()22 SkDrawable::SkDrawable() : fGenerationID(0) {}
23
draw_bbox(SkCanvas * canvas,const SkRect & r)24 static 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
draw(SkCanvas * canvas,const SkMatrix * matrix)33 void 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
draw(SkCanvas * canvas,SkScalar x,SkScalar y)45 void SkDrawable::draw(SkCanvas* canvas, SkScalar x, SkScalar y) {
46 SkMatrix matrix = SkMatrix::Translate(x, y);
47 this->draw(canvas, &matrix);
48 }
49
newPictureSnapshot()50 SkPicture* SkDrawable::newPictureSnapshot() {
51 return this->onNewPictureSnapshot();
52 }
53
getGenerationID()54 uint32_t SkDrawable::getGenerationID() {
55 if (0 == fGenerationID) {
56 fGenerationID = next_generation_id();
57 }
58 return fGenerationID;
59 }
60
getBounds()61 SkRect SkDrawable::getBounds() {
62 return this->onGetBounds();
63 }
64
approximateBytesUsed()65 size_t SkDrawable::approximateBytesUsed() {
66 return this->onApproximateBytesUsed();
67 }
onApproximateBytesUsed()68 size_t SkDrawable::onApproximateBytesUsed() {
69 return 0;
70 }
71
notifyDrawingChanged()72 void SkDrawable::notifyDrawingChanged() {
73 fGenerationID = 0;
74 }
75
76 /////////////////////////////////////////////////////////////////////////////////////////
77
78 #include "include/core/SkPictureRecorder.h"
79
onNewPictureSnapshot()80 SkPicture* SkDrawable::onNewPictureSnapshot() {
81 SkPictureRecorder recorder;
82
83 const SkRect bounds = this->getBounds();
84 SkCanvas* canvas = recorder.beginRecording(bounds);
85 this->draw(canvas);
86 if ((false)) {
87 draw_bbox(canvas, bounds);
88 }
89 return recorder.finishRecordingAsPicture().release();
90 }
91