diff --git a/engine/src/flutter/impeller/impeller/BUILD.gn b/engine/src/flutter/impeller/impeller/BUILD.gn index f7c06059ec..993e9fae20 100644 --- a/engine/src/flutter/impeller/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/BUILD.gn @@ -31,7 +31,29 @@ source_set("impeller") { "geometry/Vector.h", ] - sources = geometry_sources + entity_sources = [ + "entity/Color.cc", + "entity/Color.h", + "entity/Entity.cc", + "entity/Entity.h", + ] + + image_sources = [ + "image/DataImageSource.cc", + "image/DataImageSource.h", + "image/FileImageSource.cc", + "image/FileImageSource.h", + "image/Image.cc", + "image/Image.h", + "image/ImageEncoder.cc", + "image/ImageEncoder.h", + "image/ImageResult.cc", + "image/ImageResult.h", + "image/ImageSource.cc", + "image/ImageSource.h", + ] + + sources = geometry_sources + entity_sources + image_sources deps = [ "//flutter/fml", diff --git a/engine/src/flutter/impeller/impeller/entity/Color.cc b/engine/src/flutter/impeller/impeller/entity/Color.cc new file mode 100644 index 0000000000..ddeb79d543 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/entity/Color.cc @@ -0,0 +1,113 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include +#include +#include +#include +#include + +namespace rl { +namespace entity { + +ColorHSB ColorHSB::FromRGB(Color rgb) { + double R = rgb.red; + double G = rgb.green; + double B = rgb.blue; + + double v = 0.0; + double x = 0.0; + double f = 0.0; + + int64_t i = 0; + + x = fmin(R, G); + x = fmin(x, B); + + v = fmax(R, G); + v = fmax(v, B); + + if (v == x) + return ColorHSB(0.0, 0.0, v, rgb.alpha); + + f = (R == x) ? G - B : ((G == x) ? B - R : R - G); + i = (R == x) ? 3 : ((G == x) ? 5 : 1); + + return ColorHSB(((i - f / (v - x)) / 6.0), (v - x) / v, v, rgb.alpha); +} + +Color ColorHSB::toRGBA() const { + double h = hue * 6.0; + double s = saturation; + double v = brightness; + + double m = 0.0; + double n = 0.0; + double f = 0.0; + + int64_t i = 0; + + if (h == 0) + h = 0.01; + + if (h == 0.0) + return Color(v, v, v, alpha); + + i = static_cast(floor(h)); + + f = h - i; + + if (!(i & 1)) + f = 1 - f; + + m = v * (1 - s); + n = v * (1 - s * f); + + switch (i) { + case 6: + case 0: + return Color(v, n, m, alpha); + case 1: + return Color(n, v, m, alpha); + case 2: + return Color(m, v, n, alpha); + case 3: + return Color(m, n, v, alpha); + case 4: + return Color(n, m, v, alpha); + case 5: + return Color(v, m, n, alpha); + } + return Color(0, 0, 0, alpha); +} + +std::string ColorHSB::toString() const { + std::stringstream stream; + stream << "{" << hue << ", " << saturation << ", " << brightness << ", " + << alpha << "}"; + return stream.str(); +} + +Color::Color(const ColorHSB& hsbColor) : Color(hsbColor.toRGBA()) {} + +std::string Color::toString() const { + std::stringstream stream; + stream << red << "," << green << "," << blue << "," << alpha; + return stream.str(); +} + +void Color::fromString(const std::string& str) { + std::stringstream stream(str); + stream >> red; + stream.ignore(); + stream >> green; + stream.ignore(); + stream >> blue; + stream.ignore(); + stream >> alpha; +} + +} // namespace entity +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/entity/Color.h b/engine/src/flutter/impeller/impeller/entity/Color.h new file mode 100644 index 0000000000..a625dd281d --- /dev/null +++ b/engine/src/flutter/impeller/impeller/entity/Color.h @@ -0,0 +1,672 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include + +namespace rl { +namespace entity { + +struct ColorHSB; + +/** + * Represents a RGBA color + */ +struct Color { + /** + * The red color component (0 to 1) + */ + double red; + + /** + * The green color component (0 to 1) + */ + double green; + + /** + * The blue color component (0 to 1) + */ + double blue; + + /** + * The alpha component of the color (0 to 1) + */ + double alpha; + + Color() : red(0.0), green(0.0), blue(0.0), alpha(0.0) {} + + Color(const ColorHSB& hsbColor); + + Color(double r, double g, double b, double a) + : red(r), green(g), blue(b), alpha(a) {} + + bool operator==(const Color& c) const { + return red == c.red && green == c.green && blue == c.blue && + alpha == c.alpha; + } + + Color operator+(const Color& other) const; + + std::string toString() const; + + void fromString(const std::string& str); + + static Color White() { return {1.0, 1.0, 1.0, 1.0}; } + + static Color Black() { return {0.0, 0.0, 0.0, 1.0}; } + + static Color WhiteTransparent() { return {1.0, 1.0, 1.0, 0.0}; } + + static Color BlackTransparent() { return {0.0, 0.0, 0.0, 0.0}; } + + static Color Red() { return {1.0, 0.0, 0.0, 1.0}; } + + static Color Green() { return {0.0, 1.0, 0.0, 1.0}; } + + static Color Blue() { return {0.0, 0.0, 1.0, 1.0}; } + + static Color AliceBlue() { + return {240.0 / 255.0, 248.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color AntiqueWhite() { + return {250.0 / 255.0, 235.0 / 255.0, 215.0 / 255.0, 1.0}; + } + + static Color Aqua() { + return {0.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color AquaMarine() { + return {127.0 / 255.0, 255.0 / 255.0, 212.0 / 255.0, 1.0}; + } + + static Color Azure() { + return {240.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color Beige() { + return {245.0 / 255.0, 245.0 / 255.0, 220.0 / 255.0, 1.0}; + } + + static Color Bisque() { + return {255.0 / 255.0, 228.0 / 255.0, 196.0 / 255.0, 1.0}; + } + + static Color BlanchedAlmond() { + return {255.0 / 255.0, 235.0 / 255.0, 205.0 / 255.0, 1.0}; + } + + static Color BlueViolet() { + return {138.0 / 255.0, 43.0 / 255.0, 226.0 / 255.0, 1.0}; + } + + static Color Brown() { + return {165.0 / 255.0, 42.0 / 255.0, 42.0 / 255.0, 1.0}; + } + + static Color BurlyWood() { + return {222.0 / 255.0, 184.0 / 255.0, 135.0 / 255.0, 1.0}; + } + + static Color CadetBlue() { + return {95.0 / 255.0, 158.0 / 255.0, 160.0 / 255.0, 1.0}; + } + + static Color Chartreuse() { + return {127.0 / 255.0, 255.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color Chocolate() { + return {210.0 / 255.0, 105.0 / 255.0, 30.0 / 255.0, 1.0}; + } + + static Color Coral() { + return {255.0 / 255.0, 127.0 / 255.0, 80.0 / 255.0, 1.0}; + } + + static Color CornflowerBlue() { + return {100.0 / 255.0, 149.0 / 255.0, 237.0 / 255.0, 1.0}; + } + + static Color Cornsilk() { + return {255.0 / 255.0, 248.0 / 255.0, 220.0 / 255.0, 1.0}; + } + + static Color Crimson() { + return {220.0 / 255.0, 20.0 / 255.0, 60.0 / 255.0, 1.0}; + } + + static Color Cyan() { + return {0.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color DarkBlue() { + return {0.0 / 255.0, 0.0 / 255.0, 139.0 / 255.0, 1.0}; + } + + static Color DarkCyan() { + return {0.0 / 255.0, 139.0 / 255.0, 139.0 / 255.0, 1.0}; + } + + static Color DarkGoldenrod() { + return {184.0 / 255.0, 134.0 / 255.0, 11.0 / 255.0, 1.0}; + } + + static Color DarkGray() { + return {169.0 / 255.0, 169.0 / 255.0, 169.0 / 255.0, 1.0}; + } + + static Color DarkGreen() { + return {0.0 / 255.0, 100.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color DarkGrey() { + return {169.0 / 255.0, 169.0 / 255.0, 169.0 / 255.0, 1.0}; + } + + static Color DarkKhaki() { + return {189.0 / 255.0, 183.0 / 255.0, 107.0 / 255.0, 1.0}; + } + + static Color DarkMagenta() { + return {139.0 / 255.0, 0.0 / 255.0, 139.0 / 255.0, 1.0}; + } + + static Color DarkOliveGreen() { + return {85.0 / 255.0, 107.0 / 255.0, 47.0 / 255.0, 1.0}; + } + + static Color DarkOrange() { + return {255.0 / 255.0, 140.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color DarkOrchid() { + return {153.0 / 255.0, 50.0 / 255.0, 204.0 / 255.0, 1.0}; + } + + static Color DarkRed() { + return {139.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color DarkSalmon() { + return {233.0 / 255.0, 150.0 / 255.0, 122.0 / 255.0, 1.0}; + } + + static Color DarkSeagreen() { + return {143.0 / 255.0, 188.0 / 255.0, 143.0 / 255.0, 1.0}; + } + + static Color DarkSlateBlue() { + return {72.0 / 255.0, 61.0 / 255.0, 139.0 / 255.0, 1.0}; + } + + static Color DarkSlateGray() { + return {47.0 / 255.0, 79.0 / 255.0, 79.0 / 255.0, 1.0}; + } + + static Color DarkSlateGrey() { + return {47.0 / 255.0, 79.0 / 255.0, 79.0 / 255.0, 1.0}; + } + + static Color DarkTurquoise() { + return {0.0 / 255.0, 206.0 / 255.0, 209.0 / 255.0, 1.0}; + } + + static Color DarkViolet() { + return {148.0 / 255.0, 0.0 / 255.0, 211.0 / 255.0, 1.0}; + } + + static Color DeepPink() { + return {255.0 / 255.0, 20.0 / 255.0, 147.0 / 255.0, 1.0}; + } + + static Color DeepSkyBlue() { + return {0.0 / 255.0, 191.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color DimGray() { + return {105.0 / 255.0, 105.0 / 255.0, 105.0 / 255.0, 1.0}; + } + + static Color DimGrey() { + return {105.0 / 255.0, 105.0 / 255.0, 105.0 / 255.0, 1.0}; + } + + static Color DodgerBlue() { + return {30.0 / 255.0, 144.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color Firebrick() { + return {178.0 / 255.0, 34.0 / 255.0, 34.0 / 255.0, 1.0}; + } + + static Color FloralWhite() { + return {255.0 / 255.0, 250.0 / 255.0, 240.0 / 255.0, 1.0}; + } + + static Color ForestGreen() { + return {34.0 / 255.0, 139.0 / 255.0, 34.0 / 255.0, 1.0}; + } + + static Color Fuchsia() { + return {255.0 / 255.0, 0.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color Gainsboro() { + return {220.0 / 255.0, 220.0 / 255.0, 220.0 / 255.0, 1.0}; + } + + static Color Ghostwhite() { + return {248.0 / 255.0, 248.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color Gold() { + return {255.0 / 255.0, 215.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color Goldenrod() { + return {218.0 / 255.0, 165.0 / 255.0, 32.0 / 255.0, 1.0}; + } + + static Color Gray() { + return {128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0}; + } + + static Color GreenYellow() { + return {173.0 / 255.0, 255.0 / 255.0, 47.0 / 255.0, 1.0}; + } + + static Color Grey() { + return {128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0}; + } + + static Color Honeydew() { + return {240.0 / 255.0, 255.0 / 255.0, 240.0 / 255.0, 1.0}; + } + + static Color HotPink() { + return {255.0 / 255.0, 105.0 / 255.0, 180.0 / 255.0, 1.0}; + } + + static Color IndianRed() { + return {205.0 / 255.0, 92.0 / 255.0, 92.0 / 255.0, 1.0}; + } + + static Color Indigo() { + return {75.0 / 255.0, 0.0 / 255.0, 130.0 / 255.0, 1.0}; + } + + static Color Ivory() { + return {255.0 / 255.0, 255.0 / 255.0, 240.0 / 255.0, 1.0}; + } + + static Color Khaki() { + return {240.0 / 255.0, 230.0 / 255.0, 140.0 / 255.0, 1.0}; + } + + static Color Lavender() { + return {230.0 / 255.0, 230.0 / 255.0, 250.0 / 255.0, 1.0}; + } + + static Color LavenderBlush() { + return {255.0 / 255.0, 240.0 / 255.0, 245.0 / 255.0, 1.0}; + } + + static Color LawnGreen() { + return {124.0 / 255.0, 252.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color LemonChiffon() { + return {255.0 / 255.0, 250.0 / 255.0, 205.0 / 255.0, 1.0}; + } + + static Color LightBlue() { + return {173.0 / 255.0, 216.0 / 255.0, 230.0 / 255.0, 1.0}; + } + + static Color LightCoral() { + return {240.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0}; + } + + static Color LightCyan() { + return {224.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color LightGoldenrodYellow() { + return {50.0 / 255.0, 250.0 / 255.0, 210.0 / 255.0, 1.0}; + } + + static Color LightGray() { + return {211.0 / 255.0, 211.0 / 255.0, 211.0 / 255.0, 1.0}; + } + + static Color LightGreen() { + return {144.0 / 255.0, 238.0 / 255.0, 144.0 / 255.0, 1.0}; + } + + static Color LightGrey() { + return {211.0 / 255.0, 211.0 / 255.0, 211.0 / 255.0, 1.0}; + } + + static Color LightPink() { + return {255.0 / 255.0, 182.0 / 255.0, 193.0 / 255.0, 1.0}; + } + + static Color LightSalmon() { + return {255.0 / 255.0, 160.0 / 255.0, 122.0 / 255.0, 1.0}; + } + + static Color LightSeaGreen() { + return {32.0 / 255.0, 178.0 / 255.0, 170.0 / 255.0, 1.0}; + } + + static Color LightSkyBlue() { + return {135.0 / 255.0, 206.0 / 255.0, 250.0 / 255.0, 1.0}; + } + + static Color LightSlateGray() { + return {119.0 / 255.0, 136.0 / 255.0, 153.0 / 255.0, 1.0}; + } + + static Color LightSlateGrey() { + return {119.0 / 255.0, 136.0 / 255.0, 153.0 / 255.0, 1.0}; + } + + static Color LightSteelBlue() { + return {176.0 / 255.0, 196.0 / 255.0, 222.0 / 255.0, 1.0}; + } + + static Color LightYellow() { + return {255.0 / 255.0, 255.0 / 255.0, 224.0 / 255.0, 1.0}; + } + + static Color Lime() { return {0.0 / 255.0, 255.0 / 255.0, 0.0 / 255.0, 1.0}; } + + static Color LimeGreen() { + return {50.0 / 255.0, 205.0 / 255.0, 50.0 / 255.0, 1.0}; + } + + static Color Linen() { + return {250.0 / 255.0, 240.0 / 255.0, 230.0 / 255.0, 1.0}; + } + + static Color Magenta() { + return {255.0 / 255.0, 0.0 / 255.0, 255.0 / 255.0, 1.0}; + } + + static Color Maroon() { + return {128.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color MediumAquamarine() { + return {102.0 / 255.0, 205.0 / 255.0, 170.0 / 255.0, 1.0}; + } + + static Color MediumBlue() { + return {0.0 / 255.0, 0.0 / 255.0, 205.0 / 255.0, 1.0}; + } + + static Color MediumOrchid() { + return {186.0 / 255.0, 85.0 / 255.0, 211.0 / 255.0, 1.0}; + } + + static Color MediumPurple() { + return {147.0 / 255.0, 112.0 / 255.0, 219.0 / 255.0, 1.0}; + } + + static Color MediumSeagreen() { + return {60.0 / 255.0, 179.0 / 255.0, 113.0 / 255.0, 1.0}; + } + + static Color MediumSlateBlue() { + return {123.0 / 255.0, 104.0 / 255.0, 238.0 / 255.0, 1.0}; + } + + static Color MediumSpringGreen() { + return {0.0 / 255.0, 250.0 / 255.0, 154.0 / 255.0, 1.0}; + } + + static Color MediumTurquoise() { + return {72.0 / 255.0, 209.0 / 255.0, 204.0 / 255.0, 1.0}; + } + + static Color MediumVioletRed() { + return {199.0 / 255.0, 21.0 / 255.0, 133.0 / 255.0, 1.0}; + } + + static Color MidnightBlue() { + return {25.0 / 255.0, 25.0 / 255.0, 112.0 / 255.0, 1.0}; + } + + static Color MintCream() { + return {245.0 / 255.0, 255.0 / 255.0, 250.0 / 255.0, 1.0}; + } + + static Color MistyRose() { + return {255.0 / 255.0, 228.0 / 255.0, 225.0 / 255.0, 1.0}; + } + + static Color Moccasin() { + return {255.0 / 255.0, 228.0 / 255.0, 181.0 / 255.0, 1.0}; + } + + static Color NavajoWhite() { + return {255.0 / 255.0, 222.0 / 255.0, 173.0 / 255.0, 1.0}; + } + + static Color Navy() { return {0.0 / 255.0, 0.0 / 255.0, 128.0 / 255.0, 1.0}; } + + static Color OldLace() { + return {253.0 / 255.0, 245.0 / 255.0, 230.0 / 255.0, 1.0}; + } + + static Color Olive() { + return {128.0 / 255.0, 128.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color OliveDrab() { + return {107.0 / 255.0, 142.0 / 255.0, 35.0 / 255.0, 1.0}; + } + + static Color Orange() { + return {255.0 / 255.0, 165.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color OrangeRed() { + return {255.0 / 255.0, 69.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color Orchid() { + return {218.0 / 255.0, 112.0 / 255.0, 214.0 / 255.0, 1.0}; + } + + static Color PaleGoldenrod() { + return {238.0 / 255.0, 232.0 / 255.0, 170.0 / 255.0, 1.0}; + } + + static Color PaleGreen() { + return {152.0 / 255.0, 251.0 / 255.0, 152.0 / 255.0, 1.0}; + } + + static Color PaleTurquoise() { + return {175.0 / 255.0, 238.0 / 255.0, 238.0 / 255.0, 1.0}; + } + + static Color PaleVioletRed() { + return {219.0 / 255.0, 112.0 / 255.0, 147.0 / 255.0, 1.0}; + } + + static Color PapayaWhip() { + return {255.0 / 255.0, 239.0 / 255.0, 213.0 / 255.0, 1.0}; + } + + static Color Peachpuff() { + return {255.0 / 255.0, 218.0 / 255.0, 185.0 / 255.0, 1.0}; + } + + static Color Peru() { + return {205.0 / 255.0, 133.0 / 255.0, 63.0 / 255.0, 1.0}; + } + + static Color Pink() { + return {255.0 / 255.0, 192.0 / 255.0, 203.0 / 255.0, 1.0}; + } + + static Color Plum() { + return {221.0 / 255.0, 160.0 / 255.0, 221.0 / 255.0, 1.0}; + } + + static Color PowderBlue() { + return {176.0 / 255.0, 224.0 / 255.0, 230.0 / 255.0, 1.0}; + } + + static Color Purple() { + return {128.0 / 255.0, 0.0 / 255.0, 128.0 / 255.0, 1.0}; + } + + static Color RosyBrown() { + return {188.0 / 255.0, 143.0 / 255.0, 143.0 / 255.0, 1.0}; + } + + static Color RoyalBlue() { + return {65.0 / 255.0, 105.0 / 255.0, 225.0 / 255.0, 1.0}; + } + + static Color SaddleBrown() { + return {139.0 / 255.0, 69.0 / 255.0, 19.0 / 255.0, 1.0}; + } + + static Color Salmon() { + return {250.0 / 255.0, 128.0 / 255.0, 114.0 / 255.0, 1.0}; + } + + static Color SandyBrown() { + return {244.0 / 255.0, 164.0 / 255.0, 96.0 / 255.0, 1.0}; + } + + static Color Seagreen() { + return {46.0 / 255.0, 139.0 / 255.0, 87.0 / 255.0, 1.0}; + } + + static Color Seashell() { + return {255.0 / 255.0, 245.0 / 255.0, 238.0 / 255.0, 1.0}; + } + + static Color Sienna() { + return {160.0 / 255.0, 82.0 / 255.0, 45.0 / 255.0, 1.0}; + } + + static Color Silver() { + return {192.0 / 255.0, 192.0 / 255.0, 192.0 / 255.0, 1.0}; + } + + static Color SkyBlue() { + return {135.0 / 255.0, 206.0 / 255.0, 235.0 / 255.0, 1.0}; + } + + static Color SlateBlue() { + return {106.0 / 255.0, 90.0 / 255.0, 205.0 / 255.0, 1.0}; + } + + static Color SlateGray() { + return {112.0 / 255.0, 128.0 / 255.0, 144.0 / 255.0, 1.0}; + } + + static Color SlateGrey() { + return {112.0 / 255.0, 128.0 / 255.0, 144.0 / 255.0, 1.0}; + } + + static Color Snow() { + return {255.0 / 255.0, 250.0 / 255.0, 250.0 / 255.0, 1.0}; + } + + static Color SpringGreen() { + return {0.0 / 255.0, 255.0 / 255.0, 127.0 / 255.0, 1.0}; + } + + static Color SteelBlue() { + return {70.0 / 255.0, 130.0 / 255.0, 180.0 / 255.0, 1.0}; + } + + static Color Tan() { + return {210.0 / 255.0, 180.0 / 255.0, 140.0 / 255.0, 1.0}; + } + + static Color Teal() { + return {0.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0}; + } + + static Color Thistle() { + return {216.0 / 255.0, 191.0 / 255.0, 216.0 / 255.0, 1.0}; + } + + static Color Tomato() { + return {255.0 / 255.0, 99.0 / 255.0, 71.0 / 255.0, 1.0}; + } + + static Color Turquoise() { + return {64.0 / 255.0, 224.0 / 255.0, 208.0 / 255.0, 1.0}; + } + + static Color Violet() { + return {238.0 / 255.0, 130.0 / 255.0, 238.0 / 255.0, 1.0}; + } + + static Color Wheat() { + return {245.0 / 255.0, 222.0 / 255.0, 179.0 / 255.0, 1.0}; + } + + static Color Whitesmoke() { + return {245.0 / 255.0, 245.0 / 255.0, 245.0 / 255.0, 1.0}; + } + + static Color Yellow() { + return {255.0 / 255.0, 255.0 / 255.0, 0.0 / 255.0, 1.0}; + } + + static Color YellowGreen() { + return {154.0 / 255.0, 205.0 / 255.0, 50.0 / 255.0, 1.0}; + } +}; + +/** + * Represents a color by its constituent hue, saturation, brightness and alpha + */ +struct ColorHSB { + /** + * The hue of the color (0 to 1) + */ + double hue; + + /** + * The saturation of the color (0 to 1) + */ + double saturation; + + /** + * The brightness of the color (0 to 1) + */ + double brightness; + + /** + * The alpha of the color (0 to 1) + */ + double alpha; + + ColorHSB(double h, double s, double b, double a) + : hue(h), saturation(s), brightness(b), alpha(a) {} + + static ColorHSB FromRGB(Color rgb); + + Color toRGBA() const; + + std::string toString() const; +}; + +} // namespace entity +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/entity/Entity.cc b/engine/src/flutter/impeller/impeller/entity/Entity.cc new file mode 100644 index 0000000000..e80700e238 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/entity/Entity.cc @@ -0,0 +1,300 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include + +namespace rl { +namespace entity { + +Entity::Entity(core::Name identifier, UpdateCallback updateCallback) + : _identifier(identifier), + _anchorPoint(geom::Point(0.5, 0.5)), + _opacity(1.0), + _strokeSize(1.0), + _updateCallback(updateCallback) {} + +Entity::Entity(Entity&&) = default; + +Entity::~Entity() = default; + +void Entity::mergeProperties(const Entity& entity, PropertyMaskType only) { + RL_ASSERT(_identifier == entity._identifier); + + if (only & PropertyMask::BoundsMask) { + _bounds = entity._bounds; + } + + if (only & PropertyMask::PositionMask) { + _position = entity._position; + } + + if (only & PropertyMask::AnchorPointMask) { + _anchorPoint = entity._anchorPoint; + } + + if (only & PropertyMask::TransformationMask) { + _transformation = entity._transformation; + } + + if (only & PropertyMask::BackgroundColorMask) { + _backgroundColor = entity._backgroundColor; + } + + if (only & PropertyMask::OpacityMask) { + _opacity = entity._opacity; + } + + if (only & PropertyMask::StrokeSizeMask) { + _strokeSize = entity._strokeSize; + } + + if (only & PropertyMask::StrokeColorMask) { + _strokeColor = entity._strokeColor; + } + + if (only & PropertyMask::ContentsMask) { + _contents = entity._contents; + } + + if (only & PropertyMask::PathMask) { + _path = entity._path; + } +} + +core::Name Entity::identifier() const { + return _identifier; +} + +geom::Rect Entity::frame() const { + geom::Point origin(_position.x - (_bounds.size.width * _anchorPoint.x), + _position.y - (_bounds.size.height * _anchorPoint.y)); + + return geom::Rect(origin, _bounds.size); +} + +void Entity::setFrame(const geom::Rect& frame) { + setBounds(geom::Rect(_bounds.origin, frame.size)); + setPosition( + geom::Point(frame.origin.x + (_anchorPoint.x * frame.size.width), + frame.origin.y + (_anchorPoint.y * frame.size.height))); +} + +const geom::Rect& Entity::bounds() const { + return _bounds; +} + +void Entity::setBounds(const geom::Rect& bounds) { + _bounds = bounds; + notifyInterfaceIfNecessary(Property::Bounds); +} + +const geom::Point& Entity::position() const { + return _position; +} + +void Entity::setPosition(const geom::Point& position) { + _position = position; + notifyInterfaceIfNecessary(Property::Position); +} + +const geom::Point& Entity::anchorPoint() const { + return _anchorPoint; +} + +void Entity::setAnchorPoint(const geom::Point& anchorPoint) { + _anchorPoint = anchorPoint; + notifyInterfaceIfNecessary(Property::AnchorPoint); +} + +const geom::Matrix& Entity::transformation() const { + return _transformation; +} + +void Entity::setTransformation(const geom::Matrix& transformation) { + _transformation = transformation; + notifyInterfaceIfNecessary(Property::Transformation); +} + +geom::Matrix Entity::modelMatrix() const { + /* + * The translation accounts for the offset in the origin of the bounds + * of the entity and its position about its anchor point. + */ + auto translation = geom::Matrix::Translation( + {-_bounds.origin.x + _position.x - (_bounds.size.width * _anchorPoint.x), + -_bounds.origin.y + _position.y - + (_bounds.size.height * _anchorPoint.y)}); + + /* + * The transformation of an entity is applied about is anchor point. However, + * if the transformation is identity, we can avoid having to calculate the + * matrix adjustment and also the two matrix multiplications. + */ + + if (_transformation.isIdentity()) { + return translation; + } + + auto anchorAdjustment = + geom::Matrix::Translation({-_anchorPoint.x * _bounds.size.width, + -_anchorPoint.y * _bounds.size.height}); + + return translation * anchorAdjustment.invert() * _transformation * + anchorAdjustment; +} + +const Color& Entity::backgroundColor() const { + return _backgroundColor; +} + +void Entity::setBackgroundColor(const Color& backgroundColor) { + _backgroundColor = backgroundColor; + notifyInterfaceIfNecessary(Property::BackgroundColor); +} + +const double& Entity::opacity() const { + return _opacity; +} + +void Entity::setOpacity(double opacity) { + _opacity = opacity; + notifyInterfaceIfNecessary(Property::Opacity); +} + +const Color& Entity::strokeColor() const { + return _strokeColor; +} + +void Entity::setStrokeColor(const Color& strokeColor) { + _strokeColor = strokeColor; + notifyInterfaceIfNecessary(Property::StrokeColor); +} + +double Entity::strokeSize() const { + return _strokeSize; +} + +void Entity::setStrokeSize(double strokeSize) { + _strokeSize = strokeSize; + notifyInterfaceIfNecessary(Property::StrokeSize); +} + +const image::Image& Entity::contents() const { + return _contents; +} + +void Entity::setContents(image::Image image) { + _contents = std::move(image); + notifyInterfaceIfNecessary(Property::Contents); +} + +const geom::Path& Entity::path() const { + return _path; +} + +void Entity::setPath(geom::Path path) { + _path = std::move(path); + notifyInterfaceIfNecessary(Property::Path); +} + +void Entity::notifyInterfaceIfNecessary(Property property, + core::Name other) const { + if (_updateCallback) { + _updateCallback(*this, property, other); + } +} + +enum ArchiveKey { + Identifier, + Bounds, + Position, + AnchorPoint, + Transformation, + BackgroundColor, + Opacity, + StrokeColor, + StrokeSize, +}; + +const core::ArchiveDef Entity::ArchiveDefinition = { + /* .superClass = */ nullptr, + /* .className = */ "Entity", + /* .autoAssignName = */ false, + /* .members = */ + { + ArchiveKey::Identifier, // + ArchiveKey::Bounds, // + ArchiveKey::Position, // + ArchiveKey::AnchorPoint, // + ArchiveKey::Transformation, // + ArchiveKey::BackgroundColor, // + ArchiveKey::Opacity, // + ArchiveKey::StrokeColor, // + ArchiveKey::StrokeSize, // + }, +}; + +Entity::ArchiveName Entity::archiveName() const { + return *_identifier.handle(); +} + +bool Entity::serialize(core::ArchiveItem& item) const { + RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Identifier, _identifier)); + + RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Bounds, _bounds.toString())); + + RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Position, _position.toString())); + + RL_RETURN_IF_FALSE( + item.encode(ArchiveKey::AnchorPoint, _anchorPoint.toString())); + + RL_RETURN_IF_FALSE( + item.encode(ArchiveKey::Transformation, _transformation.toString())); + + RL_RETURN_IF_FALSE( + item.encode(ArchiveKey::BackgroundColor, _backgroundColor.toString())); + + RL_RETURN_IF_FALSE(item.encode(ArchiveKey::Opacity, _opacity)); + + RL_RETURN_IF_FALSE( + item.encode(ArchiveKey::StrokeColor, _strokeColor.toString())); + + RL_RETURN_IF_FALSE(item.encode(ArchiveKey::StrokeSize, _strokeSize)); + + return true; +} + +bool Entity::deserialize(core::ArchiveItem& item, core::Namespace* ns) { + std::string decoded; + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Identifier, _identifier, ns)); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Bounds, decoded)); + _bounds.fromString(decoded); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Position, decoded)); + _position.fromString(decoded); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::AnchorPoint, decoded)); + _anchorPoint.fromString(decoded); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Transformation, decoded)); + _transformation.fromString(decoded); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::BackgroundColor, decoded)); + _backgroundColor.fromString(decoded); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::Opacity, _opacity)); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::StrokeColor, decoded)); + _strokeColor.fromString(decoded); + + RL_RETURN_IF_FALSE(item.decode(ArchiveKey::StrokeSize, _strokeSize)); + + return true; +} + +} // namespace entity +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/entity/Entity.h b/engine/src/flutter/impeller/impeller/entity/Entity.h new file mode 100644 index 0000000000..1610304ef2 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/entity/Entity.h @@ -0,0 +1,233 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace rl { +namespace entity { + +class Entity : public core::ArchiveSerializable { + public: + using PropertyMaskType = uint16_t; + + enum class Property : PropertyMaskType { + None, + + AddedTo, + RemovedFrom, + Bounds, + Position, + AnchorPoint, + Transformation, + BackgroundColor, + Contents, + Path, + Opacity, + StrokeSize, + StrokeColor, + MakeRoot, + + Sentinel, + }; + +#define RL_MASK(x) x##Mask = (1 << static_cast(Property::x)) + enum PropertyMask { + RL_MASK(AddedTo), + RL_MASK(RemovedFrom), + RL_MASK(Bounds), + RL_MASK(Position), + RL_MASK(AnchorPoint), + RL_MASK(Transformation), + RL_MASK(BackgroundColor), + RL_MASK(Contents), + RL_MASK(Path), + RL_MASK(Opacity), + RL_MASK(StrokeSize), + RL_MASK(StrokeColor), + RL_MASK(MakeRoot), + }; +#undef RL_MASK + + using UpdateCallback = std::function; + + Entity(core::Name identifier, UpdateCallback updateCallback = nullptr); + + Entity(Entity&& entity); + + virtual ~Entity(); + + core::Name identifier() const; + + /** + * The frame specifies the origin and size of the entity in the coordinate + * space of its parent. This is a computed property derived from the bounds + * of the entity and its position. + * + * @return the frame of the entity + */ + geom::Rect frame() const; + + /** + * Set the frame of the entity + * + * @param frame the new frame + */ + void setFrame(const geom::Rect& frame); + + /** + * The bounds specifies the origin and size of the entity in its own + * coordinate space. + * + * @return the bounds of the entity + */ + const geom::Rect& bounds() const; + + /** + * Set the bounds of the entity + * + * @param bounds the new bounds + */ + void setBounds(const geom::Rect& bounds); + + /** + * The position specifies the coordinates of the anchor position of the + * entity relative to its parent + * + * @return the position of the entity + */ + const geom::Point& position() const; + + /** + * Sets the position of the entity + * + * @param point the new position + */ + void setPosition(const geom::Point& point); + + /** + * The position of the anchor point within this node in unit space + * + * @return the anchor point + */ + const geom::Point& anchorPoint() const; + + /** + * Sets the new anchor point of this node + * + * @param anchorPoint the new anchor point + */ + void setAnchorPoint(const geom::Point& anchorPoint); + + /** + * The transformation that is applied to the entity about its anchor point + * + * @return the transformation applied to the node + */ + const geom::Matrix& transformation() const; + + /** + * Sets the transformation of the entity + * + * @param transformation the new transformation + */ + void setTransformation(const geom::Matrix& transformation); + + /** + * The model matrix of the entity + * + * @return the view matrix + */ + geom::Matrix modelMatrix() const; + + /** + * The background color of the entity + * + * @return the background color + */ + const Color& backgroundColor() const; + + /** + * Set the new background color of the entity + * + * @param backgroundColor the new background color + */ + void setBackgroundColor(const Color& backgroundColor); + + /** + * The opacity of the entity. 0.0 is fully transparent and 1.0 is fully + * opaque. Default it 1.0. + * + * @return the opacity of the entity + */ + const double& opacity() const; + + /** + * Set the new opacity of the entity + * + * @param opacity the new opacity + */ + void setOpacity(double opacity); + + const Color& strokeColor() const; + + void setStrokeColor(const Color& strokeColor); + + double strokeSize() const; + + void setStrokeSize(double strokeSize); + + const image::Image& contents() const; + + void setContents(image::Image image); + + const geom::Path& path() const; + + void setPath(geom::Path path); + + void mergeProperties(const Entity& entity, PropertyMaskType only); + + static const core::ArchiveDef ArchiveDefinition; + + ArchiveName archiveName() const override; + + bool serialize(core::ArchiveItem& item) const override; + + bool deserialize(core::ArchiveItem& item, core::Namespace* ns) override; + + protected: + core::Name _identifier; + geom::Rect _bounds; + geom::Point _position; + geom::Point _anchorPoint; + geom::Matrix _transformation; + Color _backgroundColor; + image::Image _contents; + geom::Path _path; + double _opacity; + Color _strokeColor; + double _strokeSize; + + void notifyInterfaceIfNecessary( + Property property, + core::Name identifier = core::Name() /* dead name */) const; + + private: + UpdateCallback _updateCallback; + + RL_DISALLOW_COPY_AND_ASSIGN(Entity); +}; + +} // namespace entity +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/DataImageSource.cc b/engine/src/flutter/impeller/impeller/image/DataImageSource.cc new file mode 100644 index 0000000000..12d96c6029 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/DataImageSource.cc @@ -0,0 +1,51 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "DataImageSource.h" +#include + +namespace rl { +namespace image { + +DataImageSource::DataImageSource() {} + +DataImageSource::DataImageSource(core::Allocation allocation) + : _allocation(std::move(allocation)) {} + +uint8_t* DataImageSource::sourceData() const { + return _allocation.data(); +} + +size_t DataImageSource::sourceDataSize() const { + return _allocation.size(); +} + +void DataImageSource::onPrepareForUse() { + /* + * Nothing to do since we already have an explicit allocation. + */ +} + +void DataImageSource::onDoneUsing() { + /* + * Nothing to do since we have an explicit allocate and we may need to be + * prepared again. + */ +} + +bool DataImageSource::serialize(core::Message& message) const { + return message.encode(_allocation); +} + +bool DataImageSource::deserialize(core::Message& message, core::Namespace* ns) { + return message.decode(_allocation, ns); +} + +ImageSource::Type DataImageSource::type() const { + return ImageSource::Type::Data; +} + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/DataImageSource.h b/engine/src/flutter/impeller/impeller/image/DataImageSource.h new file mode 100644 index 0000000000..de25b9f6db --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/DataImageSource.h @@ -0,0 +1,42 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include "ImageSource.h" + +namespace rl { +namespace image { + +class DataImageSource : public ImageSource { + public: + DataImageSource(); + + DataImageSource(core::Allocation allocation); + + bool serialize(core::Message& message) const override; + + bool deserialize(core::Message& message, core::Namespace* ns) override; + + private: + core::Allocation _allocation; + + ImageSource::Type type() const override; + + uint8_t* sourceData() const override; + + size_t sourceDataSize() const override; + + void onPrepareForUse() override; + + void onDoneUsing() override; + + RL_DISALLOW_COPY_AND_ASSIGN(DataImageSource); +}; + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/FileImageSource.cc b/engine/src/flutter/impeller/impeller/image/FileImageSource.cc new file mode 100644 index 0000000000..cb9606cdcf --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/FileImageSource.cc @@ -0,0 +1,64 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "FileImageSource.h" +#include +#include + +namespace rl { +namespace image { + +FileImageSource::FileImageSource() {} + +FileImageSource::FileImageSource(core::FileHandle fileHandle) + : _handle(std::make_shared(std::move(fileHandle))) {} + +uint8_t* FileImageSource::sourceData() const { + return _mapping == nullptr ? nullptr : _mapping->mapping(); +} + +size_t FileImageSource::sourceDataSize() const { + return _mapping == nullptr ? 0 : _mapping->size(); +} + +void FileImageSource::onPrepareForUse() { + if (_handle == nullptr || !_handle->isValid()) { + return; + } + + _mapping = std::make_unique(*_handle); +} + +void FileImageSource::onDoneUsing() { + _mapping = nullptr; +} + +bool FileImageSource::serialize(core::Message& message) const { + return message.encode(_handle); +} + +bool FileImageSource::deserialize(core::Message& message, core::Namespace* ns) { + core::RawAttachment attachment; + + if (!message.decode(attachment)) { + return false; + } + + _handle = std::make_shared(std::move(attachment)); + + /* + * Clear our old mapping if present so we can prepare for another use. + */ + _mapping = nullptr; + + return true; +} + +ImageSource::Type FileImageSource::type() const { + return ImageSource::Type::File; +} + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/FileImageSource.h b/engine/src/flutter/impeller/impeller/image/FileImageSource.h new file mode 100644 index 0000000000..9b67cbf506 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/FileImageSource.h @@ -0,0 +1,43 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include "ImageSource.h" + +namespace rl { +namespace image { + +class FileImageSource : public ImageSource { + public: + FileImageSource(); + + FileImageSource(core::FileHandle fileHandle); + + bool serialize(core::Message& message) const override; + + bool deserialize(core::Message& message, core::Namespace* ns) override; + + private: + std::shared_ptr _handle; + std::unique_ptr _mapping; + + ImageSource::Type type() const override; + + uint8_t* sourceData() const override; + + size_t sourceDataSize() const override; + + void onPrepareForUse() override; + + void onDoneUsing() override; + + RL_DISALLOW_COPY_AND_ASSIGN(FileImageSource); +}; + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/Image.cc b/engine/src/flutter/impeller/impeller/image/Image.cc new file mode 100644 index 0000000000..35d00dea2d --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/Image.cc @@ -0,0 +1,150 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include +#include +#include +#include "ImageSource.h" + +namespace rl { +namespace image { + +Image::Image() = default; + +Image::Image(core::Allocation sourceAllocation) + : _source(ImageSource::Create(std::move(sourceAllocation))) {} + +Image::Image(core::FileHandle sourceFile) + : _source(ImageSource::Create(std::move(sourceFile))) {} + +Image::~Image() = default; + +bool Image::serialize(core::Message& message) const { + if (_source == nullptr) { + return false; + } + + /* + * Encode the type. + */ + RL_RETURN_IF_FALSE(message.encode(_source->type())); + + /* + * Encode the image source. + */ + RL_RETURN_IF_FALSE(_source->serialize(message)); + + return true; +} + +bool Image::deserialize(core::Message& message, core::Namespace* ns) { + auto type = ImageSource::Type::Unknown; + + /* + * Decode the type. + */ + RL_RETURN_IF_FALSE(message.decode(type, ns)) + + /* + * Create and decode the image source of that type. + */ + auto source = ImageSource::ImageSourceForType(type); + if (source == nullptr) { + return false; + } + RL_RETURN_IF_FALSE(source->deserialize(message, ns)); + _source = source; + + return true; +} + +ImageResult Image::decode() const { + if (_source == nullptr) { + return {}; + } + + _source->prepareForUse(); + + if (_source->sourceDataSize() == 0) { + RL_LOG("Source data for image decoding was zero sized."); + return {}; + } + + int width = 0; + int height = 0; + int comps = 0; + + stbi_uc* decoded = + stbi_load_from_memory(_source->sourceData(), // Source Data + _source->sourceDataSize(), // Source Data Size + &width, // Out: Width + &height, // Out: Height + &comps, // Out: Components + STBI_default); + + auto destinationAllocation = + core::Allocation{decoded, width * height * comps * sizeof(stbi_uc)}; + + /* + * If either the decoded allocation is null or the size works out to be zero, + * the allocation will mark itself as not ready and we know that the decode + * job failed. + */ + + if (!destinationAllocation.isReady()) { + RL_LOG("Destination allocation for image decoding was null."); + return {}; + } + + /* + * Make sure we got a valid component set. + */ + auto components = ImageResult::Components::Invalid; + + switch (comps) { + case STBI_grey: + components = ImageResult::Components::Grey; + break; + case STBI_grey_alpha: + components = ImageResult::Components::GreyAlpha; + break; + case STBI_rgb: + components = ImageResult::Components::RGB; + break; + case STBI_rgb_alpha: + components = ImageResult::Components::RGBA; + break; + default: + components = ImageResult::Components::Invalid; + break; + } + + if (components == ImageResult::Components::Invalid) { + RL_LOG("Could not detect image components when decoding."); + return {}; + } + + return ImageResult{ + geom::Size{static_cast(width), + static_cast(height)}, // size + components, // components + std::move(destinationAllocation) // allocation + }; +} + +bool Image::isValid() const { + return _source != nullptr; +} + +std::size_t Image::Hash::operator()(const Image& key) const { + return std::hash()(key._source); +} + +bool Image::Equal::operator()(const Image& lhs, const Image& rhs) const { + return lhs._source == rhs._source; +} + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/Image.h b/engine/src/flutter/impeller/impeller/image/Image.h new file mode 100644 index 0000000000..72fb12616e --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/Image.h @@ -0,0 +1,49 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include +#include + +namespace rl { +namespace image { + +class ImageSource; + +class Image : public core::MessageSerializable { + public: + Image(); + + Image(core::Allocation sourceAllocation); + + Image(core::FileHandle sourceFile); + + ~Image(); + + ImageResult decode() const; + + bool serialize(core::Message& message) const override; + + bool deserialize(core::Message& message, core::Namespace* ns) override; + + bool isValid() const; + + struct Hash { + std::size_t operator()(const Image& key) const; + }; + + struct Equal { + bool operator()(const Image& lhs, const Image& rhs) const; + }; + + private: + std::shared_ptr _source; +}; + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageEncoder.cc b/engine/src/flutter/impeller/impeller/image/ImageEncoder.cc new file mode 100644 index 0000000000..3ab55a326c --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/ImageEncoder.cc @@ -0,0 +1,86 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include +#include + +namespace rl { +namespace image { + +ImageEncoder::ImageEncoder(Type type, core::FileHandle handle) + : _isReady(false), _type(type), _adapter(std::move(handle)) { + if (!_adapter.isValid()) { + return; + } + + _isReady = true; +} + +ImageEncoder::~ImageEncoder() = default; + +bool ImageEncoder::isReady() const { + return _isReady; +} + +bool ImageEncoder::encode(ImageResult image) { + if (!_isReady) { + return false; + } + + if (!image.wasSuccessful()) { + return false; + } + + switch (_type) { + case Type::PNG: + return encodePNG(std::move(image)); + } + + return false; +} + +int ComponentsToSize(ImageResult::Components components) { + switch (components) { + case ImageResult::Components::Invalid: + return 0; + case ImageResult::Components::Grey: + return 1; + case ImageResult::Components::GreyAlpha: + return 2; + case ImageResult::Components::RGB: + return 3; + case ImageResult::Components::RGBA: + return 4; + } + + return 0; +} + +bool ImageEncoder::encodePNG(ImageResult image) { + auto size = image.size(); + + int componentSize = ComponentsToSize(image.components()); + + auto callback = [](void* context, void* data, int size) { + reinterpret_cast(context)->write( + reinterpret_cast(data), size); + }; + + return stbi_write_png_to_func(callback, // + this, // + size.width, // + size.height, // + componentSize, // + image.allocation().data(), // + componentSize // + ) == 1; +} + +void ImageEncoder::write(const uint8_t* data, size_t size) { + RL_UNUSED(_adapter.write(data, size)); +} + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageEncoder.h b/engine/src/flutter/impeller/impeller/image/ImageEncoder.h new file mode 100644 index 0000000000..ec4439b663 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/ImageEncoder.h @@ -0,0 +1,43 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include +#include + +namespace rl { +namespace image { + +class ImageEncoder { + public: + enum class Type { + PNG, + }; + + ImageEncoder(Type type, core::FileHandle handle); + + ~ImageEncoder(); + + bool isReady() const; + + bool encode(ImageResult image); + + private: + bool _isReady; + Type _type; + core::FileIOAdapter _adapter; + + bool encodePNG(ImageResult image); + + void write(const uint8_t* data, size_t size); + + RL_DISALLOW_COPY_AND_ASSIGN(ImageEncoder); +}; + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageResult.cc b/engine/src/flutter/impeller/impeller/image/ImageResult.cc new file mode 100644 index 0000000000..2fdbd9518c --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/ImageResult.cc @@ -0,0 +1,57 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include + +namespace rl { +namespace image { + +ImageResult::ImageResult(geom::Size size, + Components components, + core::Allocation allocation) + : _success(true), + _size(size), + _components(components), + _allocation(std::move(allocation)) {} + +ImageResult::ImageResult() : _success(false) {} + +ImageResult::ImageResult(ImageResult&&) = default; + +ImageResult& ImageResult::operator=(ImageResult&& other) { + _success = other._success; + other._success = false; + + _size = other._size; + other._size = geom::Size{}; + + _components = other._components; + other._components = Components::Invalid; + + _allocation = std::move(other._allocation); + + return *this; +} + +bool ImageResult::wasSuccessful() const { + return _success; +} + +const geom::Size& ImageResult::size() const { + return _size; +} + +ImageResult::Components ImageResult::components() const { + return _components; +} + +const core::Allocation& ImageResult::allocation() const { + return _allocation; +} + +ImageResult::~ImageResult() = default; + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageResult.h b/engine/src/flutter/impeller/impeller/image/ImageResult.h new file mode 100644 index 0000000000..6fa35ca933 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/ImageResult.h @@ -0,0 +1,55 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include + +namespace rl { +namespace image { + +class ImageResult { + public: + enum class Components { + Invalid, + Grey, + GreyAlpha, + RGB, + RGBA, + }; + + ImageResult(); + + ImageResult(geom::Size size, + Components components, + core::Allocation allocation); + + ImageResult(ImageResult&&); + + ImageResult& operator=(ImageResult&&); + + ~ImageResult(); + + const geom::Size& size() const; + + bool wasSuccessful() const; + + Components components() const; + + const core::Allocation& allocation() const; + + private: + bool _success; + geom::Size _size; + Components _components; + core::Allocation _allocation; + + RL_DISALLOW_COPY_AND_ASSIGN(ImageResult); +}; + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageSource.cc b/engine/src/flutter/impeller/impeller/image/ImageSource.cc new file mode 100644 index 0000000000..8040b951c8 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/ImageSource.cc @@ -0,0 +1,59 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "ImageSource.h" +#include "DataImageSource.h" +#include "FileImageSource.h" + +namespace rl { +namespace image { + +std::unique_ptr ImageSource::Create(core::Allocation allocation) { + return std::make_unique(std::move(allocation)); +} + +std::unique_ptr ImageSource::Create(core::FileHandle fileHandle) { + return std::make_unique(std::move(fileHandle)); +} + +std::shared_ptr ImageSource::ImageSourceForType(Type type) { + switch (type) { + case Type::File: + return std::make_shared(); + case Type::Data: + return std::make_shared(); + default: + return nullptr; + } + + return nullptr; +} + +ImageSource::ImageSource() = default; + +ImageSource::~ImageSource() = default; + +void ImageSource::prepareForUse() { + if (_prepared) { + return; + } + + onPrepareForUse(); + + _prepared = true; +} + +void ImageSource::doneUsing() { + if (!_prepared) { + return; + } + + onDoneUsing(); + + _prepared = false; +} + +} // namespace image +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/image/ImageSource.h b/engine/src/flutter/impeller/impeller/image/ImageSource.h new file mode 100644 index 0000000000..5e8d345898 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/image/ImageSource.h @@ -0,0 +1,54 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include + +namespace rl { +namespace image { + +class ImageSource : public core::MessageSerializable { + public: + enum class Type : uint8_t { + Unknown, + File, + Data, + }; + + static std::unique_ptr Create(core::Allocation allocation); + + static std::unique_ptr Create(core::FileHandle fileHandle); + + static std::shared_ptr ImageSourceForType(Type type); + + ImageSource(); + + virtual ~ImageSource(); + + void prepareForUse(); + + void doneUsing(); + + protected: + bool _prepared = false; + + friend class Image; + + virtual Type type() const = 0; + + virtual uint8_t* sourceData() const = 0; + + virtual size_t sourceDataSize() const = 0; + + virtual void onPrepareForUse() = 0; + + virtual void onDoneUsing() = 0; +}; + +} // namespace image +} // namespace rl