| 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/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/trace_event/trace_event.h" |
| 8 #include "chrome/browser/android/vr_shell/gltf_parser.h" | 9 #include "chrome/browser/android/vr_shell/gltf_parser.h" |
| 9 #include "chrome/grit/browser_resources.h" | 10 #include "chrome/grit/browser_resources.h" |
| 10 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
| 11 #include "third_party/skia/include/core/SkRect.h" | 12 #include "third_party/skia/include/core/SkRect.h" |
| 12 #include "third_party/skia/include/core/SkSurface.h" | 13 #include "third_party/skia/include/core/SkSurface.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
| 15 | 16 |
| 16 namespace vr_shell { | 17 namespace vr_shell { |
| 17 | 18 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 101 } |
| 101 | 102 |
| 102 const gltf::Accessor* VrControllerModel::TextureCoordinateAccessor() const { | 103 const gltf::Accessor* VrControllerModel::TextureCoordinateAccessor() const { |
| 103 return Accessor(kTexCoord); | 104 return Accessor(kTexCoord); |
| 104 } | 105 } |
| 105 | 106 |
| 106 void VrControllerModel::SetBaseTexture(sk_sp<SkImage> image) { | 107 void VrControllerModel::SetBaseTexture(sk_sp<SkImage> image) { |
| 107 base_texture_ = image; | 108 base_texture_ = image; |
| 108 } | 109 } |
| 109 | 110 |
| 110 void VrControllerModel::SetTexturePatch(int state, sk_sp<SkImage> image) { | 111 void VrControllerModel::SetTexture(int state, sk_sp<SkImage> patch) { |
| 111 DCHECK(state >= 0 && state < STATE_COUNT); | 112 DCHECK(state >= 0 && state < STATE_COUNT); |
| 112 patches_[state] = image; | 113 if (!patch) { |
| 113 } | 114 textures_[state] = base_texture_; |
| 114 | 115 return; |
| 115 sk_sp<SkImage> VrControllerModel::GetTexture(int state) const { | 116 } |
| 116 if (!patches_[state]) | |
| 117 return base_texture_; | |
| 118 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( | 117 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( |
| 119 base_texture_->width(), base_texture_->height()); | 118 base_texture_->width(), base_texture_->height()); |
| 120 SkCanvas* canvas = surface->getCanvas(); | 119 SkCanvas* canvas = surface->getCanvas(); |
| 121 canvas->drawImage(base_texture_, 0, 0); | 120 canvas->drawImage(base_texture_, 0, 0); |
| 122 SkPaint paint; | 121 SkPaint paint; |
| 123 paint.setBlendMode(SkBlendMode::kSrc); | 122 paint.setBlendMode(SkBlendMode::kSrc); |
| 124 canvas->drawImage(patches_[state], kPatchesLocations[state].x(), | 123 canvas->drawImage(patch, kPatchesLocations[state].x(), |
| 125 kPatchesLocations[state].y(), &paint); | 124 kPatchesLocations[state].y(), &paint); |
| 126 return sk_sp<SkImage>(surface->makeImageSnapshot()); | 125 textures_[state] = sk_sp<SkImage>(surface->makeImageSnapshot()); |
| 126 } |
| 127 |
| 128 sk_sp<SkImage> VrControllerModel::GetTexture(int state) const { |
| 129 DCHECK(state >= 0 && state < STATE_COUNT); |
| 130 return textures_[state]; |
| 127 } | 131 } |
| 128 | 132 |
| 129 const char* VrControllerModel::Buffer() const { | 133 const char* VrControllerModel::Buffer() const { |
| 130 if (buffers_.empty()) | 134 if (buffers_.empty()) |
| 131 return nullptr; | 135 return nullptr; |
| 132 return buffers_[0]->data(); | 136 return buffers_[0]->data(); |
| 133 } | 137 } |
| 134 | 138 |
| 135 const gltf::Accessor* VrControllerModel::Accessor( | 139 const gltf::Accessor* VrControllerModel::Accessor( |
| 136 const std::string& key) const { | 140 const std::string& key) const { |
| 137 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0); | 141 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0); |
| 138 DCHECK(mesh && mesh->primitives.size()); | 142 DCHECK(mesh && mesh->primitives.size()); |
| 139 auto it = mesh->primitives[0]->attributes.find(key); | 143 auto it = mesh->primitives[0]->attributes.find(key); |
| 140 DCHECK(it != mesh->primitives[0]->attributes.begin()); | 144 DCHECK(it != mesh->primitives[0]->attributes.begin()); |
| 141 return it->second; | 145 return it->second; |
| 142 } | 146 } |
| 143 | 147 |
| 144 std::unique_ptr<VrControllerModel> VrControllerModel::LoadFromComponent() { | 148 std::unique_ptr<VrControllerModel> VrControllerModel::LoadFromResources() { |
| 149 TRACE_EVENT0("gpu", "VrControllerModel::LoadFromResources"); |
| 145 std::vector<std::unique_ptr<gltf::Buffer>> buffers; | 150 std::vector<std::unique_ptr<gltf::Buffer>> buffers; |
| 146 auto model_data = ResourceBundle::GetSharedInstance().GetRawDataResource( | 151 auto model_data = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 147 IDR_VR_SHELL_DDCONTROLLER_MODEL); | 152 IDR_VR_SHELL_DDCONTROLLER_MODEL); |
| 148 std::unique_ptr<gltf::Asset> asset = | 153 std::unique_ptr<gltf::Asset> asset = |
| 149 BinaryGltfParser::Parse(model_data, &buffers); | 154 BinaryGltfParser::Parse(model_data, &buffers); |
| 150 DCHECK(asset); | 155 DCHECK(asset); |
| 151 | 156 |
| 152 auto controller_model = | 157 auto controller_model = |
| 153 base::MakeUnique<VrControllerModel>(std::move(asset), std::move(buffers)); | 158 base::MakeUnique<VrControllerModel>(std::move(asset), std::move(buffers)); |
| 154 sk_sp<SkImage> base_texture = LoadPng(IDR_VR_SHELL_DDCONTROLLER_IDLE_TEXTURE); | 159 sk_sp<SkImage> base_texture = LoadPng(IDR_VR_SHELL_DDCONTROLLER_IDLE_TEXTURE); |
| 155 controller_model->SetBaseTexture(std::move(base_texture)); | 160 controller_model->SetBaseTexture(std::move(base_texture)); |
| 156 | 161 |
| 157 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) { | 162 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) { |
| 158 if (kTexturePatchesResources[i] == -1) | 163 if (kTexturePatchesResources[i] == -1) { |
| 159 continue; | 164 controller_model->SetTexture(i, nullptr); |
| 160 auto patch_image = LoadPng(kTexturePatchesResources[i]); | 165 } else { |
| 161 controller_model->SetTexturePatch(i, patch_image); | 166 auto patch_image = LoadPng(kTexturePatchesResources[i]); |
| 167 controller_model->SetTexture(i, patch_image); |
| 168 } |
| 162 } | 169 } |
| 163 | 170 |
| 164 return controller_model; | 171 return controller_model; |
| 165 } | 172 } |
| 166 | 173 |
| 167 } // namespace vr_shell | 174 } // namespace vr_shell |
| OLD | NEW |