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