Skip to content

Commit 39c500e

Browse files
shlyakpavelgmta
authored andcommitted
LibGfx: Use Skia for TinyVG rendering
1 parent 331f26a commit 39c500e

File tree

5 files changed

+51
-31
lines changed

5 files changed

+51
-31
lines changed

Libraries/LibGfx/ImageFormats/TinyVGLoader.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include <AK/LEB128.h>
1111
#include <AK/MemoryStream.h>
1212
#include <AK/Variant.h>
13-
#include <LibGfx/AntiAliasingPainter.h>
14-
#include <LibGfx/DeprecatedPainter.h>
1513
#include <LibGfx/ImageFormats/TinyVGLoader.h>
1614
#include <LibGfx/Line.h>
15+
#include <LibGfx/Painter.h>
16+
#include <LibGfx/Path.h>
1717
#include <LibGfx/Point.h>
1818

1919
namespace Gfx {
@@ -254,9 +254,9 @@ class TinyVGReader {
254254
return FloatLine { TRY(read_point()), TRY(read_point()) };
255255
}
256256

257-
ErrorOr<DeprecatedPath> read_path(u32 segment_count)
257+
ErrorOr<Path> read_path(u32 segment_count)
258258
{
259-
DeprecatedPath path;
259+
Path path;
260260
auto segment_lengths = TRY(FixedArray<u32>::create(segment_count));
261261
for (auto& command_count : segment_lengths) {
262262
command_count = TRY(read_var_uint()) + 1;
@@ -366,8 +366,8 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
366366
auto color_table = TRY(decode_color_table(stream, header.color_encoding, header.color_count));
367367
TinyVGReader reader { stream, header, color_table.span() };
368368

369-
auto rectangle_to_path = [](FloatRect const& rect) -> DeprecatedPath {
370-
DeprecatedPath path;
369+
auto rectangle_to_path = [](FloatRect const& rect) -> Path {
370+
Path path;
371371
path.move_to({ rect.x(), rect.y() });
372372
path.line_to({ rect.x() + rect.width(), rect.y() });
373373
path.line_to({ rect.x() + rect.width(), rect.y() + rect.height() });
@@ -389,7 +389,7 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
389389
break;
390390
case Command::FillPolygon: {
391391
auto header = TRY(reader.read_fill_command_header(style_type));
392-
DeprecatedPath polygon;
392+
Path polygon;
393393
polygon.move_to(TRY(reader.read_point()));
394394
for (u32 i = 0; i < header.count - 1; i++)
395395
polygon.line_to(TRY(reader.read_point()));
@@ -412,7 +412,7 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
412412
}
413413
case Command::DrawLines: {
414414
auto header = TRY(reader.read_draw_command_header(style_type));
415-
DeprecatedPath path;
415+
Path path;
416416
for (u32 i = 0; i < header.count; i++) {
417417
auto line = TRY(reader.read_line());
418418
path.move_to(line.a());
@@ -424,7 +424,7 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
424424
case Command::DrawLineStrip:
425425
case Command::DrawLineLoop: {
426426
auto header = TRY(reader.read_draw_command_header(style_type));
427-
DeprecatedPath path;
427+
Path path;
428428
path.move_to(TRY(reader.read_point()));
429429
for (u32 i = 0; i < header.count - 1; i++)
430430
path.line_to(TRY(reader.read_point()));
@@ -441,7 +441,7 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
441441
}
442442
case Command::OutlineFillPolygon: {
443443
auto header = TRY(reader.read_outline_fill_command_header(style_type));
444-
DeprecatedPath polygon;
444+
Path polygon;
445445
polygon.move_to(TRY(reader.read_point()));
446446
for (u32 i = 0; i < header.count - 1; i++)
447447
polygon.line_to(TRY(reader.read_point()));
@@ -471,29 +471,29 @@ ErrorOr<NonnullRefPtr<TinyVGDecodedImageData>> TinyVGDecodedImageData::decode(St
471471
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) TinyVGDecodedImageData({ header.width, header.height }, move(draw_commands))));
472472
}
473473

474-
void TinyVGDecodedImageData::draw_transformed(DeprecatedPainter& painter, AffineTransform transform) const
474+
void TinyVGDecodedImageData::draw_transformed(Painter& painter, AffineTransform transform) const
475475
{
476476
// FIXME: Correctly handle non-uniform scales.
477477
auto scale = max(transform.x_scale(), transform.y_scale());
478-
AntiAliasingPainter aa_painter { painter };
479478
for (auto const& command : draw_commands()) {
480479
auto draw_path = command.path.copy_transformed(transform);
481480
if (command.fill.has_value()) {
482481
auto fill_path = draw_path;
483482
fill_path.close_all_subpaths();
484483
command.fill->visit(
485-
[&](Color color) { aa_painter.fill_path(fill_path, color, WindingRule::EvenOdd); },
484+
[&](Color color) { painter.fill_path(fill_path, color, WindingRule::EvenOdd); },
486485
[&](NonnullRefPtr<SVGGradientPaintStyle> style) {
487486
const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
488-
aa_painter.fill_path(fill_path, style, 1.0f, WindingRule::EvenOdd);
487+
painter.fill_path(fill_path, style, 1.0f, WindingRule::EvenOdd);
489488
});
490489
}
490+
491491
if (command.stroke.has_value()) {
492492
command.stroke->visit(
493-
[&](Color color) { aa_painter.stroke_path(draw_path, color, command.stroke_width * scale); },
493+
[&](Color color) { painter.stroke_path(draw_path, color, command.stroke_width * scale); },
494494
[&](NonnullRefPtr<SVGGradientPaintStyle> style) {
495495
const_cast<SVGGradientPaintStyle&>(*style).set_gradient_transform(transform);
496-
aa_painter.stroke_path(draw_path, style, command.stroke_width * scale);
496+
painter.stroke_path(draw_path, style, command.stroke_width * scale, 1.0f);
497497
});
498498
}
499499
}

Libraries/LibGfx/ImageFormats/TinyVGLoader.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#include <AK/OwnPtr.h>
1111
#include <AK/Vector.h>
1212
#include <LibGfx/Color.h>
13-
#include <LibGfx/DeprecatedPath.h>
1413
#include <LibGfx/Forward.h>
1514
#include <LibGfx/ImageFormats/ImageDecoder.h>
1615
#include <LibGfx/PaintStyle.h>
16+
#include <LibGfx/Painter.h>
17+
#include <LibGfx/Path.h>
1718
#include <LibGfx/VectorGraphic.h>
1819

1920
namespace Gfx {
@@ -41,7 +42,7 @@ class TinyVGDecodedImageData final : public VectorGraphic {
4142
using Style = Variant<Color, NonnullRefPtr<SVGGradientPaintStyle>>;
4243

4344
struct DrawCommand {
44-
DeprecatedPath path;
45+
Path path;
4546
Optional<Style> fill {};
4647
Optional<Style> stroke {};
4748
float stroke_width { 0.0f };
@@ -52,7 +53,7 @@ class TinyVGDecodedImageData final : public VectorGraphic {
5253
return m_size;
5354
}
5455

55-
virtual void draw_transformed(DeprecatedPainter&, AffineTransform) const override;
56+
virtual void draw_transformed(Painter&, AffineTransform) const override;
5657

5758
ReadonlySpan<DrawCommand> draw_commands() const
5859
{

Libraries/LibGfx/VectorGraphic.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

7-
#include <LibGfx/DeprecatedPainter.h>
7+
#include <LibGfx/PainterSkia.h>
88
#include <LibGfx/VectorGraphic.h>
99

1010
namespace Gfx {
1111

12-
void VectorGraphic::draw_into(DeprecatedPainter& painter, IntRect const& dest, AffineTransform transform) const
12+
void VectorGraphic::draw_into(Painter& painter, IntRect const& dest, AffineTransform transform) const
1313
{
1414
// Apply the transform then center within destination rectangle (this ignores any translation from the transform):
1515
// This allows you to easily rotate or flip the image before painting.
@@ -21,14 +21,14 @@ void VectorGraphic::draw_into(DeprecatedPainter& painter, IntRect const& dest, A
2121
.multiply(AffineTransform {}.scale(scale, scale))
2222
.multiply(AffineTransform {}.translate(-transformed_rect.location()))
2323
.multiply(transform);
24-
return draw_transformed(painter, view_transform);
24+
draw_transformed(painter, view_transform);
2525
}
2626

2727
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const
2828
{
2929
auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size));
30-
DeprecatedPainter painter { *bitmap };
31-
draw_into(painter, IntRect { {}, size }, transform);
30+
auto painter = PainterSkia::create(bitmap);
31+
draw_into(*painter, IntRect { {}, size }, transform);
3232
return bitmap;
3333
}
3434

Libraries/LibGfx/VectorGraphic.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
#include <AK/RefCounted.h>
1010
#include <LibGfx/Bitmap.h>
1111
#include <LibGfx/Forward.h>
12+
#include <LibGfx/Painter.h>
1213
#include <LibGfx/Size.h>
1314

1415
namespace Gfx {
1516

1617
class VectorGraphic : public RefCounted<VectorGraphic> {
1718
public:
1819
virtual IntSize intrinsic_size() const = 0;
19-
virtual void draw_transformed(DeprecatedPainter&, AffineTransform) const = 0;
20+
virtual void draw_transformed(Painter&, AffineTransform) const = 0;
2021

2122
IntSize size() const { return intrinsic_size(); }
2223
IntRect rect() const { return { {}, size() }; }
2324

2425
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap(IntSize size, AffineTransform = {}) const;
25-
void draw_into(DeprecatedPainter&, IntRect const& dest, AffineTransform = {}) const;
26+
void draw_into(Painter& painter, IntRect const& dest, AffineTransform = {}) const;
2627

2728
virtual ~VectorGraphic() = default;
2829
};

UI/Qt/TVGIconEngine.cpp

+23-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
#include <AK/MemoryStream.h>
88
#include <AK/String.h>
9-
#include <LibGfx/DeprecatedPainter.h>
9+
#include <LibGfx/PainterSkia.h>
10+
#include <LibGfx/Rect.h>
1011
#include <UI/Qt/StringUtils.h>
1112
#include <UI/Qt/TVGIconEngine.h>
1213

@@ -34,15 +35,32 @@ QPixmap TVGIconEngine::pixmap(QSize const& size, QIcon::Mode mode, QIcon::State
3435
if (QPixmapCache::find(key, &pixmap))
3536
return pixmap;
3637
auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { size.width(), size.height() }));
37-
Gfx::DeprecatedPainter painter { *bitmap };
38-
m_image_data->draw_into(painter, bitmap->rect());
38+
39+
auto painter = Gfx::PainterSkia::create(bitmap);
40+
painter->clear_rect(bitmap->rect().to_type<float>(), Gfx::Color::Transparent);
41+
42+
m_image_data->draw_into(*painter, bitmap->rect());
43+
3944
for (auto const& filter : m_filters) {
4045
if (filter->mode() == mode) {
41-
painter.blit_filtered({}, *bitmap, bitmap->rect(), filter->function(), false);
46+
for (int y = 0; y < bitmap->height(); ++y) {
47+
for (int x = 0; x < bitmap->width(); ++x) {
48+
auto original_color = bitmap->get_pixel(x, y);
49+
auto filtered_color = filter->function()(original_color);
50+
bitmap->set_pixel(x, y, filtered_color);
51+
}
52+
}
4253
break;
4354
}
4455
}
45-
QImage qimage { bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), QImage::Format::Format_ARGB32 };
56+
57+
QImage qimage(
58+
bitmap->scanline_u8(0),
59+
bitmap->width(),
60+
bitmap->height(),
61+
static_cast<qsizetype>(bitmap->pitch()),
62+
QImage::Format::Format_ARGB32);
63+
4664
pixmap = QPixmap::fromImage(qimage);
4765
if (!pixmap.isNull())
4866
QPixmapCache::insert(key, pixmap);

0 commit comments

Comments
 (0)