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/ui_element_renderer.h" | 10 #include "chrome/browser/android/vr_shell/ui_element_renderer.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 TRACE_EVENT0("gpu", "TexturedElement::Initialize"); | 22 TRACE_EVENT0("gpu", "TexturedElement::Initialize"); |
23 glGenTextures(1, &texture_handle_); | 23 glGenTextures(1, &texture_handle_); |
24 DCHECK(GetTexture() != nullptr); | 24 DCHECK(GetTexture() != nullptr); |
25 texture_size_ = GetTexture()->GetPreferredTextureSize(maximum_width_); | 25 texture_size_ = GetTexture()->GetPreferredTextureSize(maximum_width_); |
26 initialized_ = true; | 26 initialized_ = true; |
27 UpdateTexture(); | 27 UpdateTexture(); |
28 set_fill(Fill::SELF); | 28 set_fill(Fill::SELF); |
29 } | 29 } |
30 | 30 |
31 void TexturedElement::UpdateTexture() { | 31 void TexturedElement::UpdateTexture() { |
32 if (!initialized_ || !GetTexture()->dirty()) | 32 if (!initialized_ || !GetTexture()->dirty() || !IsVisible()) |
33 return; | 33 return; |
34 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( | 34 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( |
35 texture_size_.width(), texture_size_.height()); | 35 texture_size_.width(), texture_size_.height()); |
36 GetTexture()->DrawAndLayout(surface->getCanvas(), texture_size_); | 36 GetTexture()->DrawAndLayout(surface->getCanvas(), texture_size_); |
37 Flush(surface.get()); | 37 Flush(surface.get()); |
38 // Update the element size's aspect ratio to match the texture. | 38 // Update the element size's aspect ratio to match the texture. |
39 UpdateElementSize(); | 39 UpdateElementSize(); |
40 } | 40 } |
41 | 41 |
42 void TexturedElement::UpdateElementSize() { | 42 void TexturedElement::UpdateElementSize() { |
43 // Updating the height according to width is a hack. This may be overridden. | 43 // Updating the height according to width is a hack. This may be overridden. |
44 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); | 44 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); |
45 float y = drawn_size.height() / drawn_size.width() * size().x(); | 45 float y = drawn_size.height() / drawn_size.width() * size().x(); |
46 set_size({size().x(), y, 1}); | 46 set_size({size().x(), y, 1}); |
47 } | 47 } |
48 | 48 |
49 void TexturedElement::Render(UiElementRenderer* renderer, | 49 void TexturedElement::Render(UiElementRenderer* renderer, |
50 gfx::Transform view_proj_matrix) const { | 50 gfx::Transform view_proj_matrix) const { |
51 if (!initialized_) | 51 if (!initialized_) |
52 return; | 52 return; |
| 53 DCHECK(!GetTexture()->dirty()); |
53 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); | 54 gfx::SizeF drawn_size = GetTexture()->GetDrawnSize(); |
54 gfx::RectF copy_rect(0, 0, drawn_size.width() / texture_size_.width(), | 55 gfx::RectF copy_rect(0, 0, drawn_size.width() / texture_size_.width(), |
55 drawn_size.height() / texture_size_.height()); | 56 drawn_size.height() / texture_size_.height()); |
56 renderer->DrawTexturedQuad(texture_handle_, view_proj_matrix, copy_rect, | 57 renderer->DrawTexturedQuad(texture_handle_, view_proj_matrix, copy_rect, |
57 opacity()); | 58 opacity()); |
58 } | 59 } |
59 | 60 |
60 void TexturedElement::Flush(SkSurface* surface) { | 61 void TexturedElement::Flush(SkSurface* surface) { |
61 cc::SkiaPaintCanvas paint_canvas(surface->getCanvas()); | 62 cc::SkiaPaintCanvas paint_canvas(surface->getCanvas()); |
62 paint_canvas.flush(); | 63 paint_canvas.flush(); |
63 SkPixmap pixmap; | 64 SkPixmap pixmap; |
64 CHECK(surface->peekPixels(&pixmap)); | 65 CHECK(surface->peekPixels(&pixmap)); |
65 | 66 |
66 glBindTexture(GL_TEXTURE_2D, texture_handle_); | 67 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, | 72 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, |
72 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); | 73 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); |
73 } | 74 } |
74 | 75 |
75 void TexturedElement::OnSetMode() { | 76 void TexturedElement::OnSetMode() { |
76 GetTexture()->SetMode(mode()); | 77 GetTexture()->SetMode(mode()); |
77 UpdateTexture(); | 78 UpdateTexture(); |
78 } | 79 } |
79 | 80 |
| 81 void TexturedElement::OnBeginFrame(const base::TimeTicks& begin_frame_time) { |
| 82 UpdateTexture(); |
| 83 } |
| 84 |
80 } // namespace vr_shell | 85 } // namespace vr_shell |
OLD | NEW |