1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "render/rs_light_up_effect_filter.h"
16
17 #include "src/core/SkOpts.h"
18
19 #include "platform/common/rs_log.h"
20 #include "effect/color_matrix.h"
21
22 namespace OHOS {
23 namespace Rosen {
RSLightUpEffectFilter(float lightUpDegree)24 RSLightUpEffectFilter::RSLightUpEffectFilter(float lightUpDegree)
25 : RSDrawingFilterOriginal(RSLightUpEffectFilter::CreateLightUpEffectFilter(lightUpDegree)),
26 lightUpDegree_(lightUpDegree)
27 {
28 type_ = FilterType::LIGHT_UP_EFFECT;
29
30 hash_ = SkOpts::hash(&type_, sizeof(type_), 0);
31 hash_ = SkOpts::hash(&lightUpDegree_, sizeof(lightUpDegree_), hash_);
32 }
33
34 RSLightUpEffectFilter::~RSLightUpEffectFilter() = default;
35
CreateLightUpEffectFilter(float lightUpDegree)36 std::shared_ptr<Drawing::ImageFilter> RSLightUpEffectFilter::CreateLightUpEffectFilter(float lightUpDegree)
37 {
38 float normalizedDegree = lightUpDegree - 1.0;
39 const Drawing::scalar lightUp[] = {
40 1.000000f, 0.000000f, 0.000000f, 0.000000f, normalizedDegree,
41 0.000000f, 1.000000f, 0.000000f, 0.000000f, normalizedDegree,
42 0.000000f, 0.000000f, 1.000000f, 0.000000f, normalizedDegree,
43 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
44 };
45 std::shared_ptr<Drawing::ColorFilter> lightUpFilter = Drawing::ColorFilter::CreateFloatColorFilter(lightUp);
46
47 return Drawing::ImageFilter::CreateColorFilterImageFilter(*lightUpFilter, nullptr);
48 }
49
GetLightUpDegree()50 float RSLightUpEffectFilter::GetLightUpDegree()
51 {
52 return lightUpDegree_;
53 }
54
GetDescription()55 std::string RSLightUpEffectFilter::GetDescription()
56 {
57 return "RSLightUpEffectFilter light up degree is " + std::to_string(lightUpDegree_);
58 }
59
Compose(const std::shared_ptr<RSDrawingFilterOriginal> & other) const60 std::shared_ptr<RSDrawingFilterOriginal> RSLightUpEffectFilter::Compose(
61 const std::shared_ptr<RSDrawingFilterOriginal>& other) const
62 {
63 std::shared_ptr<RSLightUpEffectFilter> result = std::make_shared<RSLightUpEffectFilter>(lightUpDegree_);
64 result->imageFilter_ = Drawing::ImageFilter::CreateComposeImageFilter(imageFilter_, other->GetImageFilter());
65 auto otherHash = other->Hash();
66 result->hash_ = SkOpts::hash(&otherHash, sizeof(otherHash), hash_);
67 return result;
68 }
69
Add(const std::shared_ptr<RSFilter> & rhs)70 std::shared_ptr<RSFilter> RSLightUpEffectFilter::Add(const std::shared_ptr<RSFilter>& rhs)
71 {
72 if ((rhs == nullptr) || (rhs->GetFilterType() != FilterType::LIGHT_UP_EFFECT)) {
73 return shared_from_this();
74 }
75 auto lightUpFilter = std::static_pointer_cast<RSLightUpEffectFilter>(rhs);
76 return std::make_shared<RSLightUpEffectFilter>(lightUpDegree_ + lightUpFilter->GetLightUpDegree());
77 }
78
Sub(const std::shared_ptr<RSFilter> & rhs)79 std::shared_ptr<RSFilter> RSLightUpEffectFilter::Sub(const std::shared_ptr<RSFilter>& rhs)
80 {
81 if ((rhs == nullptr) || (rhs->GetFilterType() != FilterType::LIGHT_UP_EFFECT)) {
82 return shared_from_this();
83 }
84 auto lightUpFilter = std::static_pointer_cast<RSLightUpEffectFilter>(rhs);
85 return std::make_shared<RSLightUpEffectFilter>(lightUpDegree_ - lightUpFilter->GetLightUpDegree());
86 }
87
Multiply(float rhs)88 std::shared_ptr<RSFilter> RSLightUpEffectFilter::Multiply(float rhs)
89 {
90 return std::make_shared<RSLightUpEffectFilter>(lightUpDegree_ * rhs);
91 }
92
Negate()93 std::shared_ptr<RSFilter> RSLightUpEffectFilter::Negate()
94 {
95 return std::make_shared<RSLightUpEffectFilter>(-lightUpDegree_);
96 }
97
IsNearEqual(const std::shared_ptr<RSFilter> & other,float threshold) const98 bool RSLightUpEffectFilter::IsNearEqual(const std::shared_ptr<RSFilter>& other, float threshold) const
99 {
100 auto otherLightUpFilter = std::static_pointer_cast<RSLightUpEffectFilter>(other);
101 if (otherLightUpFilter == nullptr) {
102 ROSEN_LOGE("RSLightUpEffectFilter::IsNearEqual: the types of filters are different.");
103 return true;
104 }
105 float otherLightUpDegree = otherLightUpFilter->GetLightUpDegree();
106 return ROSEN_EQ(lightUpDegree_, otherLightUpDegree, threshold);
107 }
108
IsNearZero(float threshold) const109 bool RSLightUpEffectFilter::IsNearZero(float threshold) const
110 {
111 return ROSEN_EQ(lightUpDegree_, 0.0f, threshold);
112 }
113
IsEqual(const std::shared_ptr<RSFilter> & other) const114 bool RSLightUpEffectFilter::IsEqual(const std::shared_ptr<RSFilter>& other) const
115 {
116 auto otherLightUpFilter = std::static_pointer_cast<RSLightUpEffectFilter>(other);
117 if (otherLightUpFilter == nullptr) {
118 ROSEN_LOGE("RSLightUpEffectFilter::IsEqual: the types of filters are different.");
119 return true;
120 }
121 float otherLightUpDegree = otherLightUpFilter->GetLightUpDegree();
122 return ROSEN_EQ(lightUpDegree_, otherLightUpDegree);
123 }
124
IsEqualZero() const125 bool RSLightUpEffectFilter::IsEqualZero() const
126 {
127 return ROSEN_EQ(lightUpDegree_, 0.0f);
128 }
129 } // namespace Rosen
130 } // namespace OHOS
131