17#include "include/core/SkRefCnt.h"
18#include "include/core/SkBlendMode.h"
19#include "include/core/SkMatrix.h"
24#include "include/core/SkSamplingOptions.h"
43#if defined(SK_GRAPHITE)
44#define PULP_CANVAS_GRAPHITE 1
46#define PULP_CANVAS_GRAPHITE 0
49#define PULP_CANVAS_GANESH 1
51#define PULP_CANVAS_GANESH 0
58class SkiaCanvas :
public Canvas {
61 float backing_scale() const noexcept override;
67 class RetainedLayerStore;
68 static std::shared_ptr<RetainedLayerStore> create_retained_layer_store();
78 std::
size_t prune_abandoned_retained_layers();
83 std::
size_t retained_layer_count() const;
86 static std::
size_t max_retained_layers();
89 explicit SkiaCanvas(SkCanvas* canvas, skgpu::graphite::Recorder* recorder =
nullptr);
90 SkiaCanvas(SkCanvas* canvas,
91 skgpu::graphite::Recorder* recorder,
92 std::shared_ptr<RetainedLayerStore> retained_layers);
93 ~SkiaCanvas() override;
102 void set_gpu_upload_context(GrDirectContext* context);
105 void save() override;
106 void restore() override;
107 int save_count() const override;
108 void restore_to_count(
int target) override;
111 void translate(
float x,
float y) override;
112 void scale(
float sx,
float sy) override;
113 void rotate(
float radians) override;
114 void set_transform(
float a,
float b,
float c,
115 float d,
float e,
float f) override;
116 void capture_paint_baseline_transform() override;
117 void concat_transform(
float a,
float b,
float c,
118 float d,
float e,
float f) override;
119 AffineTransform2x3 current_transform() const override;
122 void clip_rect(
float x,
float y,
float w,
float h) override;
123 void clip(FillRule rule = FillRule::nonzero) override;
124 void clip_path_svg(const std::
string& svg_path_d) override;
127 void set_fill_color(Color c) override;
128 void set_stroke_color(Color c) override;
129 void set_line_width(
float w) override;
130 void set_line_cap(LineCap cap) override;
131 void set_line_join(LineJoin join) override;
136 void set_miter_limit(
float limit) override;
137 void set_image_smoothing(
bool enabled,
138 ImageSmoothingQuality quality) override;
141 void fill_rect(
float x,
float y,
float w,
float h) override;
142 void clear_rect(
float x,
float y,
float w,
float h) override;
143 void stroke_rect(
float x,
float y,
float w,
float h) override;
144 void fill_rounded_rect(
float x,
float y,
float w,
float h,
float radius) override;
145 void stroke_rounded_rect(
float x,
float y,
float w,
float h,
float radius) override;
146 void fill_circle(
float cx,
float cy,
float radius) override;
147 void stroke_circle(
float cx,
float cy,
float radius) override;
150 void stroke_arc(
float cx,
float cy,
float radius,
151 float start_angle,
float end_angle) override;
154 void stroke_line(
float x0,
float y0,
float x1,
float y1) override;
163 void stroke_path(const Point2D* points,
size_t count) override;
164 void fill_path(const Point2D* points,
size_t count,
165 FillRule rule = FillRule::nonzero) override;
173 using Canvas::fill_path;
174 using Canvas::stroke_path;
175 void fill_path(const Path& path, FillRule rule = FillRule::nonzero) override;
176 void stroke_path(const Path& path, const StrokeStyle& style) override;
177 void clip_path(const Path& path, FillRule rule = FillRule::nonzero) override;
178 void draw_path_shadow(const Path& path,
float dx,
float dy,
179 float blur,
float spread, Color color) override;
184 LayerHandle begin_layer(Rect2D bounds,
bool cacheable = false) override;
185 LayerHandle end_layer() override;
186 void draw_layer(LayerHandle layer,
float alpha = 1.0f,
187 BlendMode mode = BlendMode::normal) override;
188 void draw_layer_fitted(LayerHandle layer, Rect2D dest) override;
189 void draw_layer_rotated(LayerHandle layer,
float angle_rad) override;
190 bool layer_valid(LayerHandle layer) const override;
191 void invalidate_layer(LayerHandle layer) override;
200 std::shared_ptr<SceneRecording> record_scene(
201 float w,
float h, const std::function<
void(Canvas&)>& paint) override;
202 bool draw_scene(const SceneRecording& rec) override;
205 void set_font(const std::
string& family,
float size) override;
206 void set_font_full(const std::
string& family,
float size,
207 int weight,
int slant,
float letter_spacing) override;
208 void set_font_smoothing(FontSmoothing smoothing) override;
211 void set_font_features(std::vector<FontFeature> features) override;
212 void clear_font_features() override;
213 void set_text_align(TextAlign align) override;
214 void fill_text(const std::
string& text,
float x,
float y) override;
216 void fill_text_anchored(const std::
string& text,
float x,
float y,
217 TextAnchor anchor) override;
219 void fill_text_with_max_width(const std::
string& text,
220 float x,
float y,
float max_width) override;
221 void stroke_text(const std::
string& text,
float x,
float y,
222 float max_width = 0.0f) override;
223 void fill_text_sdf(const std::
string& text,
float x,
float y,
224 const SdfAtlas& atlas) override;
225 float measure_text(const std::
string& text) override;
226 TextMetrics measure_text_full(const std::
string& text) override;
230 float text_x_for_byte(const std::
string& text,
231 std::
size_t byte_index) override;
237 bool supports(CanvasCapability cap)
const override {
239 case CanvasCapability::images:
240 case CanvasCapability::svg:
241 case CanvasCapability::clip_path_svg:
242 case CanvasCapability::filter_chain:
243 case CanvasCapability::mask_layer:
244 case CanvasCapability::backdrop_filter:
245 case CanvasCapability::backdrop_filter_chain:
246 case CanvasCapability::bloom_layer:
247 case CanvasCapability::sksl_draw:
248 case CanvasCapability::sksl_post_effect:
249 case CanvasCapability::box_shadow_gaussian:
250 case CanvasCapability::scene_cache:
252 case CanvasCapability::retained_layer_cache:
253 return retained_layer_cache_supported_;
254 case CanvasCapability::count:
261 bool draw_image_from_data(
const uint8_t* data,
size_t size,
262 float x,
float y,
float w,
float h)
override;
263 bool draw_image_from_file(
const std::string& path,
264 float x,
float y,
float w,
float h)
override;
265 bool draw_svg(
const std::string& svg_document,
266 float x,
float y,
float w,
float h)
override;
271 bool draw_image_from_data_rect(
const uint8_t* data,
size_t size,
272 float sx,
float sy,
float sw,
float sh,
273 float dx,
float dy,
float dw,
float dh)
override;
274 bool draw_image_from_static_data_rect(
const uint8_t* data,
size_t size,
float sx,
float sy,
275 float sw,
float sh,
float dx,
float dy,
float dw,
279 bool draw_image_from_keyed_data(
const std::string& content_key,
const uint8_t* data,
280 size_t size,
float x,
float y,
float w,
float h)
override;
281 bool draw_image_from_keyed_data_rect(
const std::string& content_key,
const uint8_t* data,
282 size_t size,
float sx,
float sy,
float sw,
float sh,
283 float dx,
float dy,
float dw,
float dh)
override;
284 bool draw_image_from_file_rect(
const std::string& path,
285 float sx,
float sy,
float sw,
float sh,
286 float dx,
float dy,
float dw,
float dh)
override;
287 bool measure_image_from_file(
const std::string& path,
288 float& out_width,
float& out_height)
override;
290 bool measure_image_from_data(
const uint8_t* data,
size_t size,
float& out_width,
291 float& out_height)
override;
299 bool draw_skia_image(
const sk_sp<SkImage>& image,
300 float x,
float y,
float w,
float h);
303 void set_line_dash(
const float* intervals,
int count,
float phase)
override;
304 bool read_pixels(
int x,
int y,
int width,
int height, uint8_t* out)
override;
305 bool write_pixels(
const uint8_t* data,
int width,
int height,
306 int dx,
int dy)
override;
307 void draw_waveform(
const float* samples,
size_t count,
308 float x,
float y,
float width,
float height,
309 const WaveformStyle& style)
override;
311 void set_fill_gradient_linear(
float x0,
float y0,
float x1,
float y1,
312 const Color* colors,
const float* positions,
int count)
override;
313 void set_fill_gradient_radial(
float cx,
float cy,
float radius,
314 const Color* colors,
const float* positions,
int count)
override;
316 void set_fill_gradient_radial_elliptical(
317 float cx,
float cy,
float rx,
float ry,
318 const Color* colors,
const float* positions,
int count)
override;
320 void set_fill_gradient_radial_two_circles(
321 float x0,
float y0,
float r0,
322 float x1,
float y1,
float r1,
323 const Color* colors,
const float* positions,
int count)
override;
324 void set_fill_gradient_conic_repeating(
float cx,
float cy,
float start_angle,
326 const Color* colors,
const float* positions,
329 void set_fill_gradient_linear_repeating(
float x0,
float y0,
float x1,
float y1,
330 const Color* colors,
const float* positions,
332 void set_fill_gradient_conic(
float cx,
float cy,
float start_angle,
333 const Color* colors,
const float* positions,
int count)
override;
334 void clear_fill_gradient()
override;
342 void set_stroke_gradient_linear(
float x0,
float y0,
float x1,
float y1,
343 const Color* colors,
const float* positions,
int count)
override;
344 void set_stroke_gradient_radial(
float cx,
float cy,
float radius,
345 const Color* colors,
const float* positions,
int count)
override;
346 void set_stroke_gradient_radial_two_circles(
347 float x0,
float y0,
float r0,
348 float x1,
float y1,
float r1,
349 const Color* colors,
const float* positions,
int count)
override;
350 void set_stroke_gradient_conic(
float cx,
float cy,
float start_angle,
351 const Color* colors,
const float* positions,
int count)
override;
352 void clear_stroke_gradient()
override;
359 void set_fill_pattern(
const std::string& image_src,
360 PatternTileMode tile_x,
361 PatternTileMode tile_y)
override;
362 void set_stroke_pattern(
const std::string& image_src,
363 PatternTileMode tile_x,
364 PatternTileMode tile_y)
override;
367 void set_blend_mode(BlendMode mode)
override;
370 void begin_path()
override;
371 void move_to(
float x,
float y)
override;
372 void line_to(
float x,
float y)
override;
373 void quad_to(
float cpx,
float cpy,
float x,
float y)
override;
374 void cubic_to(
float cp1x,
float cp1y,
float cp2x,
float cp2y,
float x,
float y)
override;
375 void close_path()
override;
376 void fill_current_path(FillRule rule = FillRule::nonzero)
override;
377 void stroke_current_path()
override;
380 void arc(
float cx,
float cy,
float radius,
381 float start_angle,
float end_angle,
382 bool anticlockwise)
override;
383 void arc_to(
float x1,
float y1,
float x2,
float y2,
384 float radius)
override;
385 void ellipse(
float cx,
float cy,
float rx,
float ry,
387 float start_angle,
float end_angle,
388 bool anticlockwise)
override;
389 void round_rect(
float x,
float y,
float w,
float h,
390 float tl_x,
float tl_y,
391 float tr_x,
float tr_y,
392 float br_x,
float br_y,
393 float bl_x,
float bl_y)
override;
396 void draw_sdf_shape(SDFShape shape,
float x,
float y,
float w,
float h,
397 const SDFStyle& style)
override;
398 bool draw_sdf_shape_with_shader(SDFShape shape,
float x,
float y,
399 float w,
float h,
const SDFStyle& style,
400 const std::string& sksl,
401 const ShaderDrawOptions& options)
override;
402 void draw_blurred_backdrop(
float x,
float y,
float w,
float h,
403 float blur_radius,
float corner_radius,
404 Color tint)
override;
405 void save_backdrop_filter(
float x,
float y,
float w,
float h,
406 float blur_radius)
override;
407 void save_backdrop_filter_chain(
float x,
float y,
float w,
float h,
408 const FilterChainEntry* chain,
413 void draw_box_shadow(
float x,
float y,
float w,
float h,
414 float dx,
float dy,
float blur,
float spread,
415 Color color,
bool inset,
float corner_radius)
override;
421 void set_shadow_color(Color color)
override;
422 void set_shadow_blur(
float blur)
override;
423 void set_shadow_offset_x(
float dx)
override;
424 void set_shadow_offset_y(
float dy)
override;
432 void set_direction(TextDirection direction)
override;
433 void set_filter(
const std::string& filter)
override;
436 bool draw_with_sksl(
const std::string& sksl,
437 float x,
float y,
float w,
float h,
438 const ShaderUniforms& uniforms)
override;
439 bool draw_with_sksl(
const std::string& sksl,
440 float x,
float y,
float w,
float h,
441 const ShaderDrawOptions& options)
override;
445 bool save_layer_with_sksl_post_effect(
446 float x,
float y,
float w,
float h,
447 const std::string& sksl,
const ShaderUniforms& uniforms,
448 float sample_radius = 0.0f,
449 const std::vector<NamedUniform>& extra_uniforms = {},
450 BlendMode blend_mode = BlendMode::normal)
override;
453 bool draw_native_dawn_texture(
void* texture_handle,
456 const std::string& format,
467 static Canvas::TextMetrics measure_text_with_font(
468 const std::string& family,
float size,
const std::string& text);
471 void set_opacity(
float alpha)
override;
472 void save_layer(
float x,
float y,
float w,
float h,
473 float opacity,
float blur_radius)
override;
477 void save_layer_with_blend(
float x,
float y,
float w,
float h,
478 float opacity,
float blur_radius,
479 Canvas::BlendMode mode)
override;
481 void save_layer_with_filters(
float x,
float y,
float w,
float h,
483 const FilterChainEntry* chain,
491 void save_layer_with_shader_effect(
float x,
float y,
float w,
float h,
492 const std::string& effect_name,
493 float intensity)
override;
507 void save_layer_with_mask(
float x,
float y,
float w,
float h,
509 const std::string& mask_image,
510 const std::string& mask_size)
override;
511 void save_layer_with_bloom(
float x,
float y,
float w,
float h,
512 float intensity,
float threshold,
513 float radius)
override;
524 void push_layer(
float x,
float y,
float w,
float h,
525 float opacity,
float blur_radius, Canvas::BlendMode mode,
526 sk_sp<SkImageFilter> image_filter,
527 bool force_non_opaque =
false);
532 SkPaint current_fill_paint()
const;
540 void apply_shadow_filter(SkPaint& paint)
const;
544 bool shadow_is_active()
const;
550 void apply_stroke_state(SkPaint& paint)
const;
555 SkSamplingOptions sampling_options_for_image_smoothing()
const;
561 sk_sp<SkImage> ensure_gpu_image(sk_sp<SkImage> image)
const;
568 sk_sp<SkImage> image_from_file_cached(
const std::string& path)
const;
572 sk_sp<SkImage> image_from_keyed_data_cached(
const std::string& content_key,
const uint8_t* data,
576 const void* image_cache_backend_key()
const;
579 skgpu::graphite::Recorder* recorder_ =
nullptr;
580 GrDirectContext* gr_context_ =
nullptr;
581 Color fill_color_ = Color::rgba(1.0f, 1.0f, 1.0f);
582 Color stroke_color_ = Color::rgba(1.0f, 1.0f, 1.0f);
583 float line_width_ = 1.0f;
584 LineCap line_cap_ = LineCap::butt;
585 LineJoin line_join_ = LineJoin::miter;
588 float miter_limit_ = 10.0f;
592 bool image_smoothing_enabled_ =
true;
593 ImageSmoothingQuality image_smoothing_quality_ = ImageSmoothingQuality::low;
594 std::string font_family_ =
"sans-serif";
595 int font_weight_ = 400;
597 float letter_spacing_ = 0.0f;
599 FontSmoothing font_smoothing_ = FontSmoothing::auto_smoothing;
600 bool glyph_hinting()
const {
601 return font_smoothing_ != FontSmoothing::antialiased;
606 struct SmoothingScope {
610 std::vector<SmoothingScope> smoothing_scopes_;
612 void unwind_font_smoothing();
619 std::vector<FontFeature> font_features_;
628 sk_sp<class SkShader> shader;
630 int save_count_after_open;
632 std::vector<PendingMask> pending_masks_;
643 bool inside_non_opaque_layer()
const {
644 return !non_opaque_layer_stack_.empty();
653 std::vector<int> non_opaque_layer_stack_;
658 bool has_gradient_ =
false;
659 sk_sp<SkShader> gradient_shader_;
664 sk_sp<SkShader> stroke_shader_;
667 std::unique_ptr<SkPathBuilder> path_builder_;
670 SkBlendMode blend_mode_ = SkBlendMode::kSrcOver;
677 SkMatrix paint_baseline_ = SkMatrix::I();
682 std::vector<float> line_dash_;
683 float line_dash_phase_ = 0.0f;
692 Color shadow_color_ = Color::rgba(0.0f, 0.0f, 0.0f, 0.0f);
693 float shadow_blur_ = 0.0f;
694 float shadow_offset_x_ = 0.0f;
695 float shadow_offset_y_ = 0.0f;
707 std::string filter_source_ =
"none";
708 sk_sp<SkImageFilter> filter_image_filter_;
717 sk_sp<SkImage>
image;
718 sk_sp<SkSurface> surface;
720 float scale_x = 1.0f;
721 float scale_y = 1.0f;
722 bool cacheable =
false;
724 std::shared_ptr<RetainedLayerStore> retained_layers_;
725 bool retained_layer_cache_supported_ =
false;
731 SkCanvas* previous_canvas =
nullptr;
735 std::vector<OpenLayer> open_layers_;
737 SkiaLayer* find_layer(uint64_t
id);
738 const SkiaLayer* find_layer(uint64_t
id)
const;
739 void bind_retained_layer_store(
const void* owner, uint8_t backend_kind);
741 void draw_layer_common(LayerHandle layer,
const SkMatrix& placement,
742 float alpha, BlendMode mode);
Definition canvas.hpp:240
@ cancelled
Definition status.hpp:61
Definition affine_transform.hpp:24
TextAlign
Definition canvas.hpp:221
@ right
Definition canvas.hpp:221
@ left
Definition canvas.hpp:221
@ bottom
Definition canvas.hpp:222
@ top
Definition canvas.hpp:222
FontSmoothing
CSS -webkit-font-smoothing as it affects glyph rasterization on macOS.
Definition text_raster_policy.hpp:40
RendererBackend
Runtime identity of the concrete renderer receiving draw calls.
Definition canvas.hpp:29
TextDirection
Definition canvas.hpp:231
LineJoin
Join applied where two segments meet when stroking.
Definition path.hpp:60
@ image
Definition scene_node.hpp:95
LineCap
Line cap applied to the ends of open subpaths when stroking.
Definition path.hpp:57
canvas::Color Color
Definition theme.hpp:12
@ bounds
Definition waveform_editor_primitives.hpp:192
Definition skia_surface.hpp:16