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