[dali_2.2.14] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / buffer-definition.cpp
1 /*
2  * Copyright (c) 2023 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 INCLUDES
19 #include <dali-scene3d/public-api/loader/buffer-definition.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/builder/base64-encoding.h>
23 #include <dali/devel-api/adaptor-framework/file-stream.h>
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28 namespace Scene3D
29 {
30 namespace Loader
31 {
32 namespace
33 {
34 static constexpr std::string_view EMBEDDED_DATA_PREFIX                 = "data:";
35 static constexpr std::string_view EMBEDDED_DATA_APPLICATION_MEDIA_TYPE = "application/";
36 static constexpr std::string_view EMBEDDED_DATA_BASE64_ENCODING_TYPE   = "base64,";
37 } // namespace
38
39 struct BufferDefinition::Impl
40 {
41   std::vector<uint8_t>             buffer;
42   std::shared_ptr<Dali::FileStream> stream;
43 };
44
45 BufferDefinition::BufferDefinition()
46 : mImpl{new BufferDefinition::Impl}
47 {
48 }
49
50 BufferDefinition::~BufferDefinition() = default;
51
52 BufferDefinition::BufferDefinition(BufferDefinition&& other)
53 : mResourcePath(std::move(other.mResourcePath)),
54   mUri(std::move(other.mUri)),
55   mByteLength(std::move(other.mByteLength)),
56   mName(std::move(other.mName)),
57   mImpl(std::move(other.mImpl))
58 {
59 }
60
61 std::iostream& BufferDefinition::GetBufferStream()
62 {
63   LoadBuffer();
64   return mImpl.get()->stream.get()->GetStream();
65 }
66
67 std::string BufferDefinition::GetUri()
68 {
69   return mResourcePath + ((mIsEmbedded) ? std::string() : mUri);
70 }
71
72 bool BufferDefinition::IsAvailable()
73 {
74   LoadBuffer();
75   return mImpl.get()->stream != nullptr;
76 }
77
78 void BufferDefinition::LoadBuffer()
79 {
80   if(mImpl.get()->stream == nullptr)
81   {
82     if(mUri.find(EMBEDDED_DATA_PREFIX.data()) == 0 && mUri.find(EMBEDDED_DATA_APPLICATION_MEDIA_TYPE.data(), EMBEDDED_DATA_PREFIX.length()) == EMBEDDED_DATA_PREFIX.length())
83     {
84       uint32_t position = mUri.find(EMBEDDED_DATA_BASE64_ENCODING_TYPE.data(), EMBEDDED_DATA_PREFIX.length() + EMBEDDED_DATA_APPLICATION_MEDIA_TYPE.length());
85       if(position != std::string::npos)
86       {
87         position += EMBEDDED_DATA_BASE64_ENCODING_TYPE.length();
88         std::string data = mUri.substr(position);
89         mImpl.get()->buffer.clear();
90         Dali::Toolkit::DecodeBase64PropertyData(data, mImpl.get()->buffer);
91         mImpl.get()->stream = std::make_shared<Dali::FileStream>(reinterpret_cast<uint8_t*>(mImpl.get()->buffer.data()), mByteLength, FileStream::READ | FileStream::BINARY);
92         mIsEmbedded         = true;
93       }
94     }
95     else
96     {
97       mImpl.get()->stream = std::make_shared<Dali::FileStream>(mResourcePath + mUri, FileStream::READ | FileStream::BINARY);
98       if(mImpl.get()->stream == nullptr)
99       {
100         DALI_LOG_ERROR("Failed to load %s\n", (mResourcePath + mUri).c_str());
101       }
102     }
103   }
104 }
105
106 } // namespace Loader
107 } // namespace Scene3D
108 } // namespace Dali