xref: /aosp_15_r20/external/skia/modules/skottie/src/layers/SolidLayer.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 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 
8 #include "include/core/SkColor.h"
9 #include "include/core/SkRect.h"
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSize.h"
12 #include "include/utils/SkParse.h"
13 #include "modules/skottie/include/Skottie.h"
14 #include "modules/skottie/src/SkottieJson.h"
15 #include "modules/skottie/src/SkottiePriv.h"
16 #include "modules/sksg/include/SkSGDraw.h"
17 #include "modules/sksg/include/SkSGGeometryNode.h"
18 #include "modules/sksg/include/SkSGPaint.h"
19 #include "modules/sksg/include/SkSGRect.h"
20 #include "modules/sksg/include/SkSGRenderNode.h"
21 #include "src/utils/SkJSON.h"
22 
23 #include <cstdint>
24 #include <utility>
25 
26 namespace skottie {
27 namespace internal {
28 
attachSolidLayer(const skjson::ObjectValue & jlayer,LayerInfo * layer_info) const29 sk_sp<sksg::RenderNode> AnimationBuilder::attachSolidLayer(const skjson::ObjectValue& jlayer,
30                                                            LayerInfo* layer_info) const {
31     layer_info->fSize = SkSize::Make(ParseDefault<float>(jlayer["sw"], 0.0f),
32                                      ParseDefault<float>(jlayer["sh"], 0.0f));
33     const skjson::StringValue* hex_str = jlayer["sc"];
34     uint32_t c;
35     if (layer_info->fSize.isEmpty() ||
36         !hex_str ||
37         *hex_str->begin() != '#' ||
38         !SkParse::FindHex(hex_str->begin() + 1, &c)) {
39         this->log(Logger::Level::kError, &jlayer, "Could not parse solid layer.");
40         return nullptr;
41     }
42 
43     const SkColor color = 0xff000000 | c;
44 
45     auto solid_paint = sksg::Color::Make(color);
46     solid_paint->setAntiAlias(true);
47     this->dispatchColorProperty(solid_paint);
48 
49     return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(layer_info->fSize)),
50                             std::move(solid_paint));
51 }
52 
53 } // namespace internal
54 } // namespace skottie
55