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