sw_engine: concrete shape rendering sequence.
[platform/core/graphics/tizenvg.git] / src / lib / sw_engine / tvgSwRenderer.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17 #ifndef _TVG_SW_RENDERER_CPP_
18 #define _TVG_SW_RENDERER_CPP_
19
20 #include "tvgSwCommon.h"
21 #include "tvgSwRenderer.h"
22
23 /************************************************************************/
24 /* Internal Class Implementation                                        */
25 /************************************************************************/
26
27 static RenderInitializer renderInit;
28
29 static inline size_t COLOR(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
30 {
31     return (a << 24 | r << 16 | g << 8 | b);
32 }
33
34 /************************************************************************/
35 /* External Class Implementation                                        */
36 /************************************************************************/
37
38 bool SwRenderer::target(uint32_t* buffer, size_t stride, size_t height)
39 {
40     assert(buffer && stride > 0 && height > 0);
41
42     surface.buffer = buffer;
43     surface.stride = stride;
44     surface.height = height;
45
46     return true;
47 }
48
49
50 bool SwRenderer::render(const ShapeNode& shape, void *data)
51 {
52     SwShape* sdata = static_cast<SwShape*>(data);
53     if (!sdata) return false;
54
55     //invisible?
56     size_t r, g, b, a;
57     shape.fill(&r, &g, &b, &a);
58     if (a == 0) return true;
59
60     //TODO: Threading
61     return rasterShape(surface, *sdata, COLOR(r, g, b, a));
62 }
63
64
65 bool SwRenderer::dispose(const ShapeNode& shape, void *data)
66 {
67     SwShape* sdata = static_cast<SwShape*>(data);
68     if (!sdata) return false;
69     shapeReset(*sdata);
70     free(sdata);
71     return true;
72 }
73
74 void* SwRenderer::prepare(const ShapeNode& shape, void* data, UpdateFlag flags)
75 {
76     //prepare shape data
77     SwShape* sdata = static_cast<SwShape*>(data);
78     if (!sdata) {
79         sdata = static_cast<SwShape*>(calloc(1, sizeof(SwShape)));
80         assert(sdata);
81     }
82
83     if (flags == UpdateFlag::None) return nullptr;
84
85     //invisible?
86     size_t alpha;
87     shape.fill(nullptr, nullptr, nullptr, &alpha);
88     if (alpha == 0) return sdata;
89
90     //TODO: Threading
91     if (flags & UpdateFlag::Path) {
92         shapeReset(*sdata);
93         if (!shapeGenOutline(shape, *sdata)) return sdata;
94         if (!shapeTransformOutline(shape, *sdata)) return sdata;
95         if (!shapeGenRle(shape, *sdata)) return sdata;
96     }
97
98     return sdata;
99 }
100
101
102 int SwRenderer::init()
103 {
104     return RenderInitializer::init(renderInit, new SwRenderer);
105 }
106
107
108 int SwRenderer::term()
109 {
110     return RenderInitializer::term(renderInit);
111 }
112
113
114 size_t SwRenderer::unref()
115 {
116     return RenderInitializer::unref(renderInit);
117 }
118
119
120 size_t SwRenderer::ref()
121 {
122     return RenderInitializer::ref(renderInit);
123 }
124
125
126 SwRenderer* SwRenderer::inst()
127 {
128     return dynamic_cast<SwRenderer*>(RenderInitializer::inst(renderInit));
129 }
130
131
132 #endif /* _TVG_SW_RENDERER_CPP_ */