Change dali-scene-loader to dali-scene3d
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / parse-renderer-state.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
18 #include "dali-scene3d/public-api/loader/parse-renderer-state.h"
19 #include <cstring>
20 #include "dali/devel-api/common/map-wrapper.h"
21
22 namespace Dali
23 {
24 namespace Scene3D
25 {
26 namespace Loader
27 {
28 namespace RendererState
29 {
30 namespace
31 {
32 std::map<std::string_view, Type> COMPARISONS{
33 #define DECL_COMPARISON(x) {#x, Comparison::x}
34   DECL_COMPARISON(NEVER),
35   DECL_COMPARISON(ALWAYS),
36   DECL_COMPARISON(LESS),
37   DECL_COMPARISON(GREATER),
38   DECL_COMPARISON(EQUAL),
39   DECL_COMPARISON(NOT_EQUAL),
40   DECL_COMPARISON(LESS_EQUAL),
41   DECL_COMPARISON(GREATER_EQUAL),
42 #undef DECL_COMPARISON
43 };
44
45 Type InterpretComparison(const std::string_view& str)
46 {
47   Type value = 0x0;
48   auto iFind = COMPARISONS.find(str);
49   if(iFind != COMPARISONS.end())
50   {
51     value = iFind->second;
52   }
53   return value;
54 }
55
56 std::map<std::string_view, Type> BLEND_FACTORS{
57 #define DECL_BLEND_FACTOR(x) {#x, Dali::Scene3D::Loader::BlendFactor::x}
58   DECL_BLEND_FACTOR(ZERO),
59   DECL_BLEND_FACTOR(ONE),
60   DECL_BLEND_FACTOR(SRC_COLOR),
61   DECL_BLEND_FACTOR(ONE_MINUS_SRC_COLOR),
62   DECL_BLEND_FACTOR(SRC_ALPHA),
63   DECL_BLEND_FACTOR(ONE_MINUS_SRC_ALPHA),
64   DECL_BLEND_FACTOR(DST_ALPHA),
65   DECL_BLEND_FACTOR(ONE_MINUS_DST_ALPHA),
66   DECL_BLEND_FACTOR(DST_COLOR),
67   DECL_BLEND_FACTOR(ONE_MINUS_DST_COLOR),
68   DECL_BLEND_FACTOR(SRC_ALPHA_SATURATE),
69   DECL_BLEND_FACTOR(CONSTANT_COLOR),
70   DECL_BLEND_FACTOR(ONE_MINUS_CONSTANT_COLOR),
71   DECL_BLEND_FACTOR(CONSTANT_ALPHA),
72   DECL_BLEND_FACTOR(ONE_MINUS_CONSTANT_ALPHA),
73 #undef DECL_BLEND_FACTOR
74 };
75
76 Type InterpretBlendFactor(const std::string_view& str, uint8_t item)
77 {
78   Type value = 0x0;
79   auto iFind = BLEND_FACTORS.find(str);
80   if(iFind != BLEND_FACTORS.end())
81   {
82     value = iFind->second << (BLEND_FACTOR_BASE_SHIFT + BLEND_FACTOR_ITEM_BITS * item);
83   }
84   return value;
85 }
86
87 std::map<std::string_view, Type> BUFFER_MODES{
88 #define DECL_BUFFER_MODE(x) {#x, BufferMode::x}
89   DECL_BUFFER_MODE(NONE),
90   DECL_BUFFER_MODE(AUTO),
91   DECL_BUFFER_MODE(COLOR),
92   DECL_BUFFER_MODE(STENCIL),
93   DECL_BUFFER_MODE(COLOR_STENCIL),
94 };
95
96 Type InterpretBufferMode(const std::string_view& str)
97 {
98   Type value = 0x0;
99   auto iFind = BUFFER_MODES.find(str);
100   if(iFind != BUFFER_MODES.end())
101   {
102     value = iFind->second << BUFFER_MODE_SHIFT;
103   }
104   return value;
105 }
106
107 std::map<std::string_view, Type (*)(const std::string_view&)> RENDERER_STATE_PROCESSORS{
108   {"DEPTH_WRITE", [](const std::string_view&) -> Type { return DEPTH_WRITE; }},
109   {"DEPTH_TEST", [](const std::string_view&) -> Type { return DEPTH_TEST; }},
110   {"CULL_FRONT", [](const std::string_view&) -> Type { return CULL_FRONT; }},
111   {"CULL_BACK", [](const std::string_view&) -> Type { return CULL_BACK; }},
112   {"ALPHA_BLEND", [](const std::string_view&) -> Type { return ALPHA_BLEND; }},
113   {"DEPTH_FUNC", [](const std::string_view& arg) -> Type {
114      Type value = (arg[0] == ':') ? (InterpretComparison(std::string_view(arg.data() + 1, arg.size() - 1)) << DEPTH_FUNCTION_SHIFT) : 0x0;
115      return value;
116    }},
117   {"BLEND_SRC_RGB", [](const std::string_view& arg) -> Type {
118      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 0) : 0x0;
119      return value;
120    }},
121   {"BLEND_DST_RGB", [](const std::string_view& arg) -> Type {
122      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 1) : 0x0;
123      return value;
124    }},
125   {"BLEND_SRC_ALPHA", [](const std::string_view& arg) -> Type {
126      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 2) : 0x0;
127      return value;
128    }},
129   {"BLEND_DST_ALPHA", [](const std::string_view& arg) -> Type {
130      Type value = (arg[0] == ':') ? InterpretBlendFactor(std::string_view(arg.data() + 1, arg.size() - 1), 3) : 0x0;
131      return value;
132    }},
133   {"BUFFER_MODE", [](const std::string_view& arg) -> Type {
134      Type value = (arg[0] == ':') ? InterpretBufferMode(std::string_view(arg.data() + 1, arg.size() - 1)) : 0x0;
135      return value;
136    }},
137 };
138
139 } // namespace
140
141 Type Parse(const char* string, size_t length, StringCallback onError)
142 {
143   if(length == 0)
144   {
145     length = strlen(string);
146   }
147
148   Type value = 0x0;
149   auto iEnd  = string + length;
150   while(string != iEnd)
151   {
152     auto iNextToken = std::find(string, iEnd, '|');
153     auto iColon     = std::find(string, iNextToken, ':');
154     auto i          = RENDERER_STATE_PROCESSORS.find(std::string_view(string, iColon - string));
155     if(i != RENDERER_STATE_PROCESSORS.end() && size_t(std::distance(string, iNextToken)) >= i->first.size())
156     {
157       value |= i->second(std::string_view(string + i->first.size(), iNextToken - iColon));
158     }
159     else
160     {
161       onError("Not a valid RendererState: " + std::string(string, iNextToken));
162     }
163
164     string = iNextToken + (iNextToken != iEnd);
165   }
166
167   return value;
168 }
169
170 } // namespace RendererState
171 } // namespace Loader
172 } // namespace Scene3D
173 } // namespace Dali