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/vr_controller_model.h" | 5 #include "chrome/browser/android/vr_shell/vr_controller_model.h" |
6 | 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
7 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/path_service.h" |
8 #include "chrome/browser/android/vr_shell/gltf_parser.h" | 11 #include "chrome/browser/android/vr_shell/gltf_parser.h" |
9 #include "chrome/grit/browser_resources.h" | 12 #include "components/component_updater/component_updater_paths.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
11 #include "third_party/skia/include/core/SkRect.h" | 14 #include "third_party/skia/include/core/SkRect.h" |
12 #include "third_party/skia/include/core/SkSurface.h" | 15 #include "third_party/skia/include/core/SkSurface.h" |
13 #include "ui/base/resource/resource_bundle.h" | |
14 #include "ui/gfx/codec/png_codec.h" | 16 #include "ui/gfx/codec/png_codec.h" |
15 | 17 |
16 namespace vr_shell { | 18 namespace vr_shell { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 enum { | 22 enum { |
21 ELEMENTS_BUFFER_ID = 0, | 23 ELEMENTS_BUFFER_ID = 2, |
22 INDICES_BUFFER_ID = 1, | 24 INDICES_BUFFER_ID = 3, |
23 }; | 25 }; |
24 | 26 |
25 constexpr char kPosition[] = "POSITION"; | 27 constexpr char kPosition[] = "POSITION"; |
26 constexpr char kTexCoord[] = "TEXCOORD_0"; | 28 constexpr char kTexCoord[] = "TEXCOORD_0"; |
27 | 29 |
28 const int kTexturePatchesResources[] = { | 30 // TODO(acondor): Remove these hardcoded paths once VrShell resources |
29 -1, IDR_VR_SHELL_DDCONTROLLER_TOUCHPAD_PATCH, | 31 // are delivered through component updater. |
30 IDR_VR_SHELL_DDCONTROLLER_APP_PATCH, IDR_VR_SHELL_DDCONTROLLER_SYSTEM_PATCH, | 32 constexpr char const kComponentName[] = "VrShell"; |
| 33 constexpr char const kDefaultVersion[] = "0"; |
| 34 |
| 35 constexpr char const kModelsDirectory[] = "models"; |
| 36 constexpr char const kModelFilename[] = "ddcontroller.glb"; |
| 37 constexpr char const kTexturesDirectory[] = "tex"; |
| 38 constexpr char const kBaseTextureFilename[] = "ddcontroller_idle.png"; |
| 39 constexpr char const* kTexturePatchesFilenames[] = { |
| 40 "", "ddcontroller_touchpad.png", "ddcontroller_app.png", |
| 41 "ddcontroller_system.png", |
31 }; | 42 }; |
32 const gfx::Point kPatchesLocations[] = {{}, {5, 5}, {47, 165}, {47, 234}}; | 43 const gfx::Point kPatchesLocations[] = {{}, {5, 5}, {47, 165}, {47, 234}}; |
33 | 44 |
34 sk_sp<SkImage> LoadPng(int resource_id) { | 45 sk_sp<SkImage> LoadPng(const base::FilePath& path) { |
35 base::StringPiece data = | 46 std::string data; |
36 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | |
37 SkBitmap bitmap; | 47 SkBitmap bitmap; |
38 bool decoded = | 48 if (!base::ReadFileToString(path, &data) || |
39 gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(data.data()), | 49 !gfx::PNGCodec::Decode( |
40 data.size(), &bitmap); | 50 reinterpret_cast<const unsigned char*>(data.data()), data.size(), |
41 DCHECK(decoded); | 51 &bitmap) || |
42 DCHECK(bitmap.colorType() == kRGBA_8888_SkColorType); | 52 bitmap.colorType() != kRGBA_8888_SkColorType) { |
| 53 return nullptr; |
| 54 } |
43 return SkImage::MakeFromBitmap(bitmap); | 55 return SkImage::MakeFromBitmap(bitmap); |
44 } | 56 } |
45 | 57 |
46 } // namespace | 58 } // namespace |
47 | 59 |
48 VrControllerModel::VrControllerModel( | 60 VrControllerModel::VrControllerModel( |
49 std::unique_ptr<gltf::Asset> gltf_asset, | 61 std::unique_ptr<gltf::Asset> gltf_asset, |
50 std::vector<std::unique_ptr<gltf::Buffer>> buffers) | 62 std::vector<std::unique_ptr<gltf::Buffer>> buffers) |
51 : gltf_asset_(std::move(gltf_asset)), | 63 : gltf_asset_(std::move(gltf_asset)), |
52 buffers_(std::move(buffers)) {} | 64 buffers_(std::move(buffers)) {} |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 const gltf::Accessor* VrControllerModel::Accessor( | 147 const gltf::Accessor* VrControllerModel::Accessor( |
136 const std::string& key) const { | 148 const std::string& key) const { |
137 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0); | 149 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0); |
138 DCHECK(mesh && mesh->primitives.size()); | 150 DCHECK(mesh && mesh->primitives.size()); |
139 auto it = mesh->primitives[0]->attributes.find(key); | 151 auto it = mesh->primitives[0]->attributes.find(key); |
140 DCHECK(it != mesh->primitives[0]->attributes.begin()); | 152 DCHECK(it != mesh->primitives[0]->attributes.begin()); |
141 return it->second; | 153 return it->second; |
142 } | 154 } |
143 | 155 |
144 std::unique_ptr<VrControllerModel> VrControllerModel::LoadFromComponent() { | 156 std::unique_ptr<VrControllerModel> VrControllerModel::LoadFromComponent() { |
| 157 base::FilePath models_path; |
| 158 PathService::Get(component_updater::DIR_COMPONENT_USER, &models_path); |
| 159 models_path = models_path.Append(kComponentName) |
| 160 .Append(kDefaultVersion) |
| 161 .Append(kModelsDirectory); |
| 162 auto model_path = models_path.Append(kModelFilename); |
| 163 |
| 164 // No further action if model file is not present |
| 165 if (!base::PathExists(model_path)) { |
| 166 LOG(WARNING) << "Controller model files not found"; |
| 167 return nullptr; |
| 168 } |
| 169 |
145 std::vector<std::unique_ptr<gltf::Buffer>> buffers; | 170 std::vector<std::unique_ptr<gltf::Buffer>> buffers; |
146 auto model_data = ResourceBundle::GetSharedInstance().GetRawDataResource( | 171 |
147 IDR_VR_SHELL_DDCONTROLLER_MODEL); | 172 std::string model_data; |
148 std::unique_ptr<gltf::Asset> asset = | 173 base::ReadFileToString(model_path, &model_data); |
149 BinaryGltfParser::Parse(model_data, &buffers); | 174 auto asset = BinaryGltfParser::Parse(base::StringPiece(model_data), &buffers); |
150 DCHECK(asset); | 175 if (!asset) { |
| 176 LOG(ERROR) << "Failed to read controller model"; |
| 177 return nullptr; |
| 178 } |
151 | 179 |
152 auto controller_model = | 180 auto controller_model = |
153 base::MakeUnique<VrControllerModel>(std::move(asset), std::move(buffers)); | 181 base::MakeUnique<VrControllerModel>(std::move(asset), std::move(buffers)); |
154 sk_sp<SkImage> base_texture = LoadPng(IDR_VR_SHELL_DDCONTROLLER_IDLE_TEXTURE); | 182 |
| 183 auto textures_path = models_path.Append(kTexturesDirectory); |
| 184 |
| 185 auto base_texture = LoadPng(textures_path.Append(kBaseTextureFilename)); |
| 186 if (!base_texture) { |
| 187 LOG(ERROR) << "Failed to read controller base texture"; |
| 188 return nullptr; |
| 189 } |
155 controller_model->SetBaseTexture(std::move(base_texture)); | 190 controller_model->SetBaseTexture(std::move(base_texture)); |
156 | 191 |
157 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) { | 192 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) { |
158 if (kTexturePatchesResources[i] == -1) | 193 if (!kTexturePatchesFilenames[i][0]) |
159 continue; | 194 continue; |
160 auto patch_image = LoadPng(kTexturePatchesResources[i]); | 195 auto patch_image = |
| 196 LoadPng(textures_path.Append(kTexturePatchesFilenames[i])); |
| 197 if (!patch_image) { |
| 198 LOG(ERROR) << "Failed to read controller texture patch"; |
| 199 continue; |
| 200 } |
161 controller_model->SetTexturePatch(i, patch_image); | 201 controller_model->SetTexturePatch(i, patch_image); |
162 } | 202 } |
163 | 203 |
164 return controller_model; | 204 return controller_model; |
165 } | 205 } |
166 | 206 |
167 } // namespace vr_shell | 207 } // namespace vr_shell |
OLD | NEW |