OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/gltf_parser.h" | 5 #include "chrome/browser/android/vr_shell/gltf_parser.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback_helpers.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/json/json_file_value_serializer.h" |
8 #include "base/logging.h" | 12 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "net/base/data_url.h" |
| 15 #include "net/base/filename_util.h" |
| 16 #include "url/gurl.h" |
10 | 17 |
11 namespace vr_shell { | 18 namespace vr_shell { |
12 | 19 |
13 constexpr char kBase64Header[] = "data:application/octet-stream;base64,"; | |
14 constexpr size_t kBase64HeaderSize = 37; | |
15 | |
16 GltfParser::GltfParser() {} | 20 GltfParser::GltfParser() {} |
17 | 21 |
18 GltfParser::~GltfParser() = default; | 22 GltfParser::~GltfParser() = default; |
19 | 23 |
20 std::unique_ptr<gltf::Asset> GltfParser::Parse( | 24 std::unique_ptr<gltf::Asset> GltfParser::Parse( |
21 const base::DictionaryValue& dict) { | 25 const base::DictionaryValue& dict, |
22 std::string gltf_version; | 26 const base::FilePath& path) { |
23 if (!dict.GetString("asset.version", &gltf_version) || gltf_version != "1.0") | 27 path_ = path; |
24 return nullptr; | |
25 | |
26 asset_ = base::MakeUnique<gltf::Asset>(); | 28 asset_ = base::MakeUnique<gltf::Asset>(); |
27 | 29 |
28 if (!ParseInternal(dict)) { | 30 base::ScopedClosureRunner runner( |
29 asset_.reset(); | 31 base::Bind(&GltfParser::Clear, base::Unretained(this))); |
| 32 |
| 33 if (!ParseInternal(dict)) |
30 return nullptr; | 34 return nullptr; |
31 } | |
32 | 35 |
33 return std::move(asset_); | 36 return std::move(asset_); |
34 } | 37 } |
35 | 38 |
| 39 std::unique_ptr<gltf::Asset> GltfParser::Parse( |
| 40 const base::FilePath& gltf_path) { |
| 41 JSONFileValueDeserializer json_deserializer(gltf_path); |
| 42 int error_code; |
| 43 std::string error_msg; |
| 44 auto asset_value = json_deserializer.Deserialize(&error_code, &error_msg); |
| 45 if (!asset_value) |
| 46 return nullptr; |
| 47 base::DictionaryValue* asset; |
| 48 if (!asset_value->GetAsDictionary(&asset)) |
| 49 return nullptr; |
| 50 return Parse(*asset, gltf_path); |
| 51 } |
| 52 |
36 bool GltfParser::ParseInternal(const base::DictionaryValue& dict) { | 53 bool GltfParser::ParseInternal(const base::DictionaryValue& dict) { |
| 54 std::string gltf_version; |
| 55 if (!dict.GetString("asset.version", &gltf_version) || gltf_version != "1.0") |
| 56 return false; |
| 57 |
37 const base::DictionaryValue* sub_dict; | 58 const base::DictionaryValue* sub_dict; |
38 if (dict.GetDictionary("buffers", &sub_dict) && !SetBuffers(*sub_dict)) | 59 if (dict.GetDictionary("buffers", &sub_dict) && !SetBuffers(*sub_dict)) |
39 return false; | 60 return false; |
40 if (dict.GetDictionary("bufferViews", &sub_dict) && | 61 if (dict.GetDictionary("bufferViews", &sub_dict) && |
41 !SetBufferViews(*sub_dict)) | 62 !SetBufferViews(*sub_dict)) |
42 return false; | 63 return false; |
43 if (dict.GetDictionary("accessors", &sub_dict) && !SetAccessors(*sub_dict)) | 64 if (dict.GetDictionary("accessors", &sub_dict) && !SetAccessors(*sub_dict)) |
44 return false; | 65 return false; |
45 if (dict.GetDictionary("meshes", &sub_dict) && !SetMeshes(*sub_dict)) | 66 if (dict.GetDictionary("meshes", &sub_dict) && !SetMeshes(*sub_dict)) |
46 return false; | 67 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
59 | 80 |
60 return true; | 81 return true; |
61 } | 82 } |
62 | 83 |
63 bool GltfParser::SetBuffers(const base::DictionaryValue& dict) { | 84 bool GltfParser::SetBuffers(const base::DictionaryValue& dict) { |
64 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 85 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
65 const base::DictionaryValue* buffer_dict; | 86 const base::DictionaryValue* buffer_dict; |
66 if (!it.value().GetAsDictionary(&buffer_dict)) | 87 if (!it.value().GetAsDictionary(&buffer_dict)) |
67 return false; | 88 return false; |
68 | 89 |
69 std::string uri; | 90 std::string uri_str; |
70 // TODO(acondor): Support files. Only inline data is supported now. | 91 if (!buffer_dict->GetString("uri", &uri_str)) |
71 if (!buffer_dict->GetString("uri", &uri) || | |
72 uri.substr(0, kBase64HeaderSize) != kBase64Header) | |
73 return false; | 92 return false; |
74 | 93 auto buffer = ProcessUri(uri_str); |
75 auto buffer = base::MakeUnique<gltf::Buffer>(); | 94 if (!buffer) |
76 if (!base::Base64Decode(uri.substr(kBase64HeaderSize), buffer.get())) | |
77 return false; | 95 return false; |
78 | 96 |
79 int byte_length; | 97 int byte_length; |
80 if (buffer_dict->GetInteger("byteLength", &byte_length) && | 98 if (buffer_dict->GetInteger("byteLength", &byte_length) && |
81 static_cast<int>(buffer->length()) != byte_length) | 99 static_cast<int>(buffer->length()) != byte_length) |
82 return false; | 100 return false; |
83 | 101 |
84 buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); | 102 buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); |
85 } | 103 } |
86 return true; | 104 return true; |
87 } | 105 } |
88 | 106 |
| 107 std::unique_ptr<gltf::Buffer> GltfParser::ProcessUri( |
| 108 const std::string& uri_str) { |
| 109 auto uri = path_.empty() ? GURL(uri_str) |
| 110 : net::FilePathToFileURL(path_).Resolve(uri_str); |
| 111 if (!uri.is_valid()) |
| 112 return nullptr; |
| 113 if (uri.SchemeIs(url::kDataScheme)) { |
| 114 std::string mime_type; |
| 115 std::string charset; |
| 116 auto data = base::MakeUnique<gltf::Buffer>(); |
| 117 if (!net::DataURL::Parse(uri, &mime_type, &charset, data.get())) |
| 118 return nullptr; |
| 119 return data; |
| 120 } |
| 121 if (uri.SchemeIsFile()) { |
| 122 auto data = base::MakeUnique<gltf::Buffer>(); |
| 123 if (!base::ReadFileToString(base::FilePath(uri.path()), data.get())) |
| 124 return nullptr; |
| 125 return data; |
| 126 } |
| 127 // No other schemes are supported yet. |
| 128 return nullptr; |
| 129 } |
| 130 |
89 bool GltfParser::SetBufferViews(const base::DictionaryValue& dict) { | 131 bool GltfParser::SetBufferViews(const base::DictionaryValue& dict) { |
90 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 132 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
91 const base::DictionaryValue* buffer_view_dict; | 133 const base::DictionaryValue* buffer_view_dict; |
92 if (!it.value().GetAsDictionary(&buffer_view_dict)) | 134 if (!it.value().GetAsDictionary(&buffer_view_dict)) |
93 return false; | 135 return false; |
94 | 136 |
95 auto buffer_view = base::MakeUnique<gltf::BufferView>(); | 137 auto buffer_view = base::MakeUnique<gltf::BufferView>(); |
96 std::string buffer_key; | 138 std::string buffer_key; |
97 if (!buffer_view_dict->GetString("buffer", &buffer_key)) | 139 if (!buffer_view_dict->GetString("buffer", &buffer_key)) |
98 return false; | 140 return false; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 return false; | 306 return false; |
265 scene->nodes.push_back(asset_->GetNode(node_it->second)); | 307 scene->nodes.push_back(asset_->GetNode(node_it->second)); |
266 } | 308 } |
267 } | 309 } |
268 | 310 |
269 scene_ids_[it.key()] = asset_->AddScene(std::move(scene)); | 311 scene_ids_[it.key()] = asset_->AddScene(std::move(scene)); |
270 } | 312 } |
271 return true; | 313 return true; |
272 } | 314 } |
273 | 315 |
| 316 void GltfParser::Clear() { |
| 317 asset_.reset(); |
| 318 path_.clear(); |
| 319 buffer_ids_.clear(); |
| 320 buffer_view_ids_.clear(); |
| 321 accessor_ids_.clear(); |
| 322 node_ids_.clear(); |
| 323 mesh_ids_.clear(); |
| 324 scene_ids_.clear(); |
| 325 } |
| 326 |
274 } // namespace vr_shell | 327 } // namespace vr_shell |
OLD | NEW |