| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/vr_shell/textures/ui_texture.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gl/gl_bindings.h" |
| 15 |
| 16 namespace vr_shell { |
| 17 |
| 18 namespace { |
| 19 |
| 20 constexpr float kDefaultLineSpacePercentage = 0.25; |
| 21 const std::string kWhitespaceChars(base::kWhitespaceASCII); |
| 22 |
| 23 std::vector<std::string> SplitText(const std::string& text, |
| 24 const SkPaint& paint, |
| 25 float max_width) { |
| 26 std::vector<std::string> lines; |
| 27 size_t first_char = 0; |
| 28 while (first_char < text.size()) { |
| 29 SkScalar width; |
| 30 size_t char_break = first_char + paint.breakText(text.c_str() + first_char, |
| 31 text.size() - first_char, |
| 32 max_width, &width); |
| 33 if (char_break == text.size()) { |
| 34 lines.push_back(text.substr(first_char)); |
| 35 break; |
| 36 } |
| 37 size_t char_break_guess = char_break + 1; |
| 38 while (char_break_guess > first_char && |
| 39 kWhitespaceChars.find(text[char_break_guess]) == std::string::npos) |
| 40 char_break_guess--; |
| 41 |
| 42 if (char_break_guess > first_char) |
| 43 char_break = char_break_guess; |
| 44 |
| 45 lines.push_back(text.substr(first_char, char_break - first_char)); |
| 46 |
| 47 first_char = char_break + (char_break_guess > first_char ? 1 : 0); |
| 48 } |
| 49 return lines; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 54 // TODO(acondor): Create non-square textures to reduce memory usage. |
| 55 UITexture::UITexture(int texture_handle, int texture_size) |
| 56 : texture_handle_(texture_handle), |
| 57 texture_size_(texture_size), |
| 58 surface_(SkSurface::MakeRasterN32Premul(texture_size_, texture_size_)) {} |
| 59 |
| 60 UITexture::~UITexture() = default; |
| 61 |
| 62 void UITexture::DrawAndLayout() { |
| 63 cc::SkiaPaintCanvas paint_canvas(surface_->getCanvas()); |
| 64 gfx::Canvas canvas(&paint_canvas, 1.0f); |
| 65 canvas.DrawColor(0x00000000); |
| 66 Draw(&canvas); |
| 67 SetSize(); |
| 68 } |
| 69 |
| 70 void UITexture::Flush() { |
| 71 cc::SkiaPaintCanvas paint_canvas(surface_->getCanvas()); |
| 72 paint_canvas.flush(); |
| 73 SkPixmap pixmap; |
| 74 CHECK(surface_->peekPixels(&pixmap)); |
| 75 |
| 76 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
| 77 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, |
| 78 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); |
| 79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 83 } |
| 84 |
| 85 void UITexture::DrawText(gfx::Canvas* canvas, |
| 86 const std::string& text, |
| 87 const cc::PaintFlags& flags, |
| 88 float max_width, |
| 89 bool v_centered) { |
| 90 // TODO(acondor): Make use of gfx::Canvas::DrawStringRect instead. |
| 91 // Platform specific font rendering capabilities need to be ported to Android. |
| 92 |
| 93 const auto& paint = cc::ToSkPaint(flags); |
| 94 |
| 95 auto lines = SplitText(text, paint, max_width); |
| 96 if (lines.empty()) |
| 97 return; |
| 98 |
| 99 float v_pos = flags.getTextSize(); |
| 100 if (v_centered) { |
| 101 float height = |
| 102 (lines.size() + (lines.size() - 1) * kDefaultLineSpacePercentage) * |
| 103 flags.getTextSize(); |
| 104 v_pos -= height * .5; |
| 105 } |
| 106 for (const auto& line : lines) { |
| 107 canvas->sk_canvas()->drawText(line.c_str(), line.size(), 0, v_pos, flags); |
| 108 v_pos += flags.getTextSize() * (1 + kDefaultLineSpacePercentage); |
| 109 } |
| 110 } |
| 111 |
| 112 } // namespace vr_shell |
| OLD | NEW |