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