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