Flutter Engine
The Flutter Engine
SkDrawable.cpp
Go to the documentation of this file.
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 */
8
12#include "include/core/SkPicture.h" // IWYU pragma: keep
14#include "include/core/SkRect.h"
17
18#include <atomic>
19#include <cstddef>
20#include <cstdint>
21
22static 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
32SkDrawable::SkDrawable() : fGenerationID(0) {}
33
34static void draw_bbox(SkCanvas* canvas, const SkRect& r) {
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
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
57 this->draw(canvas, &matrix);
58}
59
61 return this->onMakePictureSnapshot();
62}
63
65 if (0 == fGenerationID) {
66 fGenerationID = next_generation_id();
67 }
68 return fGenerationID;
69}
70
72 return this->onGetBounds();
73}
74
76 return this->onApproximateBytesUsed();
77}
79 return 0;
80}
81
83 fGenerationID = 0;
84}
85
86/////////////////////////////////////////////////////////////////////////////////////////
87
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}
static int32_t next_generation_id()
Definition: SkDrawable.cpp:22
static void draw_bbox(SkCanvas *canvas, const SkRect &r)
Definition: SkDrawable.cpp:34
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
void notifyDrawingChanged()
Definition: SkDrawable.cpp:82
uint32_t getGenerationID()
Definition: SkDrawable.cpp:64
size_t approximateBytesUsed()
Definition: SkDrawable.cpp:75
virtual SkRect onGetBounds()=0
virtual sk_sp< SkPicture > onMakePictureSnapshot()
Definition: SkDrawable.cpp:88
SkRect getBounds()
Definition: SkDrawable.cpp:71
sk_sp< SkPicture > makePictureSnapshot()
Definition: SkDrawable.cpp:60
virtual void onDraw(SkCanvas *)=0
virtual size_t onApproximateBytesUsed()
Definition: SkDrawable.cpp:78
void draw(SkCanvas *, const SkMatrix *=nullptr)
Definition: SkDrawable.cpp:43
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
Optional< SkRect > bounds
Definition: SkRecords.h:189
constexpr float left() const
Definition: SkRect.h:734
constexpr float top() const
Definition: SkRect.h:741
constexpr float right() const
Definition: SkRect.h:748
constexpr float bottom() const
Definition: SkRect.h:755
const uintptr_t id