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