Merge "DALi Version 2.0.32" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / utils.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 // INTERNAL
19 #include "dali-scene-loader/public-api/utils.h"
20
21 // EXTERNAL
22 #include <stdarg.h>
23 #include <cstring>
24 #include <fstream>
25 #include <iostream>
26 #include "dali/public-api/animation/constraints.h"
27 #include "dali/public-api/common/vector-wrapper.h"
28
29 namespace Dali
30 {
31 namespace SceneLoader
32 {
33 namespace
34 {
35 thread_local char sExceptionFlingerMessageBuffer[ExceptionFlinger::MESSAGE_BUFFER_SIZE]{};
36 }
37
38 StreamBuffer::StreamBuffer(char* buffer, size_t size) noexcept(true)
39 {
40   setp(buffer, buffer + size);
41 }
42
43 ExceptionFlinger::Impl::~Impl() noexcept(false)
44 {
45   throw DaliException(mLocation, GetMessageBuffer());
46 }
47
48 ExceptionFlinger::ExceptionFlinger(const char* location) noexcept(true)
49 : mImpl{location},
50   mStreamBuffer(GetMessageBuffer(), MESSAGE_BUFFER_SIZE - 1),
51   mStream(&mStreamBuffer)
52 {
53 }
54
55 ExceptionFlinger::~ExceptionFlinger() noexcept(false)
56 {
57   operator<<('\0');
58 }
59
60 char* ExceptionFlinger::GetMessageBuffer() noexcept(true)
61 {
62   return sExceptionFlingerMessageBuffer;
63 }
64
65 std::string FormatString(const char* format, ...)
66 {
67   va_list vl;
68   va_start(vl, format);
69   va_list vl2;
70   va_copy(vl2, vl);
71
72   size_t sizeRequired = vsnprintf(nullptr, 0, format, vl);
73   va_end(vl);
74
75   ++sizeRequired;
76   std::string result(sizeRequired, '\0');
77   vsnprintf(&result[0], sizeRequired, format, vl2);
78   va_end(vl2);
79
80   return result;
81 }
82
83 std::string LoadTextFile(const char* path, bool* fail)
84 {
85   std::ifstream inFile(path);
86   if(inFile)
87   {
88     std::istreambuf_iterator<char> eos;
89     std::istreambuf_iterator<char> i(inFile.rdbuf());
90     return std::string(i, eos);
91   }
92   else if(fail)
93   {
94     *fail = true;
95   }
96   return std::string();
97 }
98
99 Geometry MakeTexturedQuadGeometry(TexturedQuadOptions::Type options)
100 {
101   Property::Map properties;
102   properties.Insert("aPosition", Property::VECTOR3);
103   properties.Insert("aTexCoord", Property::VECTOR2);
104
105   std::vector<uint8_t> bytes;
106   size_t               stride   = 0;
107   size_t               uvOffset = 0;
108   struct
109   {
110     Vector3 aPosition;
111     Vector2 aTexCoord;
112   } vertices[] = {
113     {Vector3(-0.5f, 0.5f, 0.0f), Vector2(0.0f, .0f)},
114     {Vector3(0.5f, 0.5f, 0.0f), Vector2(1.0f, .0f)},
115     {Vector3(-0.5f, -0.5f, 0.0f), Vector2(0.0f, 1.0f)},
116     {Vector3(0.5f, -0.5f, 0.0f), Vector2(1.0f, 1.0f)}};
117
118   bytes.resize(sizeof(vertices));
119   stride   = sizeof(vertices[0]);
120   uvOffset = reinterpret_cast<const uint8_t*>(&vertices[0].aTexCoord) - reinterpret_cast<const uint8_t*>(&vertices[0]);
121
122   std::memcpy(bytes.data(), vertices, sizeof(vertices));
123
124   if(MaskMatch(options, TexturedQuadOptions::FLIP_VERTICAL))
125   {
126     Vector2* uv = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(bytes.data()) + uvOffset);
127     for(int i = 0; i < 4; ++i)
128     {
129       uv->y = 1.0f - uv->y;
130       uv    = reinterpret_cast<Vector2*>(reinterpret_cast<uint8_t*>(uv) + stride);
131     }
132   }
133
134   VertexBuffer vertexBuffer = VertexBuffer::New(properties);
135   vertexBuffer.SetData(bytes.data(), bytes.size() / stride);
136
137   Geometry geometry = Geometry::New();
138   geometry.AddVertexBuffer(vertexBuffer);
139   geometry.SetType(Geometry::TRIANGLE_STRIP);
140   return geometry;
141 }
142
143 void ToUnixFileSeparators(std::string& path)
144 {
145   std::replace(path.begin(), path.end(), '\\', '/');
146 }
147
148 } // namespace SceneLoader
149 } // namespace Dali