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