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