2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-scene3d/public-api/loader/buffer-definition.h>
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>
26 namespace Dali::Scene3D::Loader
30 static constexpr std::string_view EMBEDDED_DATA_PREFIX = "data:";
31 static constexpr std::string_view EMBEDDED_DATA_APPLICATION_MEDIA_TYPE = "application/";
32 static constexpr std::string_view EMBEDDED_DATA_BASE64_ENCODING_TYPE = "base64,";
35 struct BufferDefinition::Impl
37 std::vector<uint8_t> buffer;
38 std::shared_ptr<Dali::FileStream> stream;
41 BufferDefinition::BufferDefinition(std::vector<uint8_t>& buffer)
42 : mImpl{new BufferDefinition::Impl}
44 mImpl.get()->buffer = std::move(buffer);
45 mImpl.get()->stream = std::make_shared<Dali::FileStream>(reinterpret_cast<uint8_t*>(mImpl.get()->buffer.data()), mImpl.get()->buffer.size(), FileStream::READ | FileStream::BINARY);
49 BufferDefinition::BufferDefinition()
50 : mImpl{new BufferDefinition::Impl}
54 BufferDefinition::~BufferDefinition() = default;
56 BufferDefinition::BufferDefinition(BufferDefinition&& other)
57 : mResourcePath(std::move(other.mResourcePath)),
58 mUri(std::move(other.mUri)),
59 mByteLength(std::move(other.mByteLength)),
60 mName(std::move(other.mName)),
61 mImpl(std::move(other.mImpl))
65 std::iostream& BufferDefinition::GetBufferStream()
68 return mImpl.get()->stream.get()->GetStream();
71 std::string BufferDefinition::GetUri()
73 return mResourcePath + ((mIsEmbedded) ? std::string() : mUri);
76 bool BufferDefinition::IsAvailable()
79 return mImpl.get()->stream != nullptr;
82 void BufferDefinition::LoadBuffer()
84 if(mImpl.get()->stream == nullptr)
86 if(mUri.find(EMBEDDED_DATA_PREFIX.data()) == 0 && mUri.find(EMBEDDED_DATA_APPLICATION_MEDIA_TYPE.data(), EMBEDDED_DATA_PREFIX.length()) == EMBEDDED_DATA_PREFIX.length())
88 uint32_t position = mUri.find(EMBEDDED_DATA_BASE64_ENCODING_TYPE.data(), EMBEDDED_DATA_PREFIX.length() + EMBEDDED_DATA_APPLICATION_MEDIA_TYPE.length());
89 if(position != std::string::npos)
91 position += EMBEDDED_DATA_BASE64_ENCODING_TYPE.length();
92 std::string_view data = std::string_view(mUri).substr(position);
93 mImpl.get()->buffer.clear();
94 Dali::Toolkit::DecodeBase64FromString(data, mImpl.get()->buffer);
95 mImpl.get()->stream = std::make_shared<Dali::FileStream>(reinterpret_cast<uint8_t*>(mImpl.get()->buffer.data()), mByteLength, FileStream::READ | FileStream::BINARY);
101 mImpl.get()->stream = std::make_shared<Dali::FileStream>(mResourcePath + mUri, FileStream::READ | FileStream::BINARY);
102 if(mImpl.get()->stream == nullptr)
104 DALI_LOG_ERROR("Failed to load %s\n", (mResourcePath + mUri).c_str());
110 } // namespace Dali::Scene3D::Loader