| 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/ui_elements/textured_element.h" | 5 #include "chrome/browser/android/vr_shell/ui_elements/textured_element.h" |
| 6 | 6 |
| 7 #include "base/trace_event/trace_event.h" | 7 #include "base/trace_event/trace_event.h" |
| 8 #include "cc/paint/skia_paint_canvas.h" | 8 #include "cc/paint/skia_paint_canvas.h" |
| 9 #include "chrome/browser/android/vr_shell/textures/ui_texture.h" | 9 #include "chrome/browser/android/vr_shell/textures/ui_texture.h" |
| 10 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 10 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 11 #include "third_party/skia/include/core/SkSurface.h" | 11 #include "third_party/skia/include/core/SkSurface.h" |
| 12 | 12 |
| 13 namespace vr_shell { | 13 namespace vr_shell { |
| 14 | 14 |
| 15 TexturedElement::TexturedElement(int maximum_width) | 15 TexturedElement::TexturedElement(int maximum_width) |
| 16 : texture_handle_(-1), maximum_width_(maximum_width) {} | 16 : texture_handle_(-1), maximum_width_(maximum_width) {} |
| 17 | 17 |
| 18 TexturedElement::~TexturedElement() = default; | 18 TexturedElement::~TexturedElement() = default; |
| 19 | 19 |
| 20 void TexturedElement::Initialize() { | 20 void TexturedElement::Initialize() { |
| 21 TRACE_EVENT0("gpu", "TexturedElement::Initialize"); | 21 TRACE_EVENT0("gpu", "TexturedElement::Initialize"); |
| 22 glGenTextures(1, &texture_handle_); | 22 glGenTextures(1, &texture_handle_); |
| 23 DCHECK(GetTexture() != nullptr); | 23 DCHECK(GetTexture() != nullptr); |
| 24 texture_size_ = GetTexture()->GetPreferredTextureSize(maximum_width_); | 24 texture_size_ = GetTexture()->GetPreferredTextureSize(maximum_width_); |
| 25 initialized_ = true; |
| 25 UpdateTexture(); | 26 UpdateTexture(); |
| 26 set_fill(Fill::SELF); | 27 set_fill(Fill::SELF); |
| 27 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); | 28 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); |
| 28 float y = drawn_size.height() / drawn_size.width() * size().x(); | 29 float y = drawn_size.height() / drawn_size.width() * size().x(); |
| 29 set_size({size().x(), y, 1}); | 30 set_size({size().x(), y, 1}); |
| 30 } | 31 } |
| 31 | 32 |
| 32 void TexturedElement::UpdateTexture() { | 33 void TexturedElement::UpdateTexture() { |
| 34 if (!initialized_) |
| 35 return; |
| 33 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( | 36 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( |
| 34 texture_size_.width(), texture_size_.height()); | 37 texture_size_.width(), texture_size_.height()); |
| 35 GetTexture()->DrawAndLayout(surface->getCanvas(), texture_size_); | 38 GetTexture()->DrawAndLayout(surface->getCanvas(), texture_size_); |
| 36 Flush(surface.get()); | 39 Flush(surface.get()); |
| 37 } | 40 } |
| 38 | 41 |
| 39 void TexturedElement::Render(VrShellRenderer* renderer, | 42 void TexturedElement::Render(VrShellRenderer* renderer, |
| 40 vr::Mat4f view_proj_matrix) const { | 43 vr::Mat4f view_proj_matrix) const { |
| 44 if (!initialized_) |
| 45 return; |
| 41 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); | 46 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); |
| 42 gfx::RectF copy_rect(0, 0, drawn_size.width() / texture_size_.width(), | 47 gfx::RectF copy_rect(0, 0, drawn_size.width() / texture_size_.width(), |
| 43 drawn_size.height() / texture_size_.height()); | 48 drawn_size.height() / texture_size_.height()); |
| 44 renderer->GetTexturedQuadRenderer()->AddQuad( | 49 renderer->GetTexturedQuadRenderer()->AddQuad( |
| 45 texture_handle_, view_proj_matrix, copy_rect, opacity()); | 50 texture_handle_, view_proj_matrix, copy_rect, opacity()); |
| 46 } | 51 } |
| 47 | 52 |
| 48 void TexturedElement::Flush(SkSurface* surface) { | 53 void TexturedElement::Flush(SkSurface* surface) { |
| 49 cc::SkiaPaintCanvas paint_canvas(surface->getCanvas()); | 54 cc::SkiaPaintCanvas paint_canvas(surface->getCanvas()); |
| 50 paint_canvas.flush(); | 55 paint_canvas.flush(); |
| 51 SkPixmap pixmap; | 56 SkPixmap pixmap; |
| 52 CHECK(surface->peekPixels(&pixmap)); | 57 CHECK(surface->peekPixels(&pixmap)); |
| 53 | 58 |
| 54 glBindTexture(GL_TEXTURE_2D, texture_handle_); | 59 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
| 55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 59 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, | 64 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, |
| 60 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); | 65 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); |
| 61 } | 66 } |
| 62 | 67 |
| 63 } // namespace vr_shell | 68 } // namespace vr_shell |
| OLD | NEW |