1 /*
2  * Copyright (c) 2022 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 
16 /**
17  * @file fill_base.h
18  * @brief Defines Scan line distributor and converter
19  * @since 1.0
20  * @version 1.0
21  */
22 
23 #ifndef GRAPHIC_LITE_FILL_BASE_H
24 #define GRAPHIC_LITE_FILL_BASE_H
25 #include "gfx_utils/color.h"
26 #include "gfx_utils/diagram/vertexprimitive/geometry_plaindata_array.h"
27 namespace OHOS {
28 class FillBase {
29 public:
30     /**
31      * @brief Resize Redistribution calculation spans_ length
32      * @param span_len Scan line length
33      * @return First address of spans
34      */
Resize(uint32_t spanLen)35     inline Rgba8T* Resize(uint32_t spanLen)
36     {
37         if (spanLen > spans_.GetSize()) {
38             spans_.Resize(((spanLen + MAX_COLOR_NUM) >> BYTE_LENGTH) << BYTE_LENGTH);
39         }
40         return spans_.Data();
41     }
42 
GetSpanPtr()43     inline Rgba8T* GetSpanPtr()
44     {
45         if (spans_.GetSize() <= 0) {
46             spans_.Resize(8);
47         }
48         return &spans_[0];
49     }
50 
51 private:
52     GeometryPlainDataArray<Rgba8T> spans_;
53 };
54 
55 class SpanBase {
56 public:
57     virtual void Prepare() = 0;
58     virtual  void Generate(Rgba8T* span, int32_t, int32_t, uint32_t len) = 0;
59 };
60 
61 class SpanSoildColor : public SpanBase {
62 public:
SpanSoildColor(Rgba8T color)63     explicit SpanSoildColor(Rgba8T color)
64     {
65         color_ = color;
66     }
Prepare()67     void Prepare() {}
Generate(Rgba8T * span,int32_t,int32_t,uint32_t len)68     void Generate(Rgba8T* span, int32_t, int32_t, uint32_t len)
69     {
70         for (; len; --len) {
71             *span++ = color_;
72         }
73     }
74 private:
75     Rgba8T color_;
76 };
77 
78 class SpanBlendColor : public SpanBase {
79 public:
SpanBlendColor(SpanBase & spanGenerator1,SpanBase & spanGenerator2)80     SpanBlendColor(SpanBase& spanGenerator1, SpanBase& spanGenerator2)
81         : spanGenerator1_(spanGenerator1),
82           spanGenerator2_(spanGenerator2)
83     {
84     }
Prepare()85     void Prepare()
86     {
87         spanGenerator1_.Prepare();
88         spanGenerator2_.Prepare();
89     }
Generate(Rgba8T * span,int32_t x,int32_t y,uint32_t len)90     void Generate(Rgba8T* span, int32_t x, int32_t y, uint32_t len)
91     {
92         FillBase allocator1;
93         FillBase allocator2;
94 
95         Rgba8T* colors1 = allocator1.Resize(len);
96         Rgba8T* colors2 = allocator2.Resize(len);
97 
98         spanGenerator1_.Generate(colors1, x, y, len);
99         spanGenerator2_.Generate(colors2, x, y, len);
100 
101         for (; len; --len, ++span, colors1++, colors2++) {
102             span->red = (colors1->red+colors2->red) >= MAX_COLOR_NUM ?
103                         MAX_COLOR_NUM:(colors1->red+colors2->red);
104             span->green = (colors1->green+colors2->green) >= MAX_COLOR_NUM ?
105                         MAX_COLOR_NUM:(colors1->green+colors2->green);
106             span->blue = (colors1->blue+colors2->blue) >= MAX_COLOR_NUM ?
107                         MAX_COLOR_NUM:(colors1->blue+colors2->blue);
108             span->alpha = (colors1->alpha+colors2->alpha) >= MAX_COLOR_NUM ?
109                         MAX_COLOR_NUM:(colors1->alpha+colors2->alpha);
110         }
111     }
112 private:
113     SpanBase& spanGenerator1_;
114     SpanBase& spanGenerator2_;
115 };
116 } // namespace OHOS
117 #endif
118