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
notifyDrawingChanged()65 void SkDrawable::notifyDrawingChanged() {
66 fGenerationID = 0;
67 }
68
69 /////////////////////////////////////////////////////////////////////////////////////////
70
71 #include "include/core/SkPictureRecorder.h"
72
onNewPictureSnapshot()73 SkPicture* SkDrawable::onNewPictureSnapshot() {
74 SkPictureRecorder recorder;
75
76 const SkRect bounds = this->getBounds();
77 SkCanvas* canvas = recorder.beginRecording(bounds);
78 this->draw(canvas);
79 if (false) {
80 draw_bbox(canvas, bounds);
81 }
82 return recorder.finishRecordingAsPicture().release();
83 }
84