Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Unified Diff: chrome/browser/android/vr_shell/textures/ui_texture.cc

Issue 2818553002: Rendering Insecure WebVR Warnings into a texture with Skia. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/vr_shell/textures/ui_texture.cc
diff --git a/chrome/browser/android/vr_shell/textures/ui_texture.cc b/chrome/browser/android/vr_shell/textures/ui_texture.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b34fa1157a16de4320454146f112ca1447ef0b0f
--- /dev/null
+++ b/chrome/browser/android/vr_shell/textures/ui_texture.cc
@@ -0,0 +1,112 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/android/vr_shell/textures/ui_texture.h"
+
+#include <string>
+#include <vector>
+
+#include "base/memory/ptr_util.h"
+#include "base/strings/string_util.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gl/gl_bindings.h"
+
+namespace vr_shell {
+
+namespace {
+
+constexpr float kDefaultLineSpacePercentage = 0.25;
+const std::string kWhitespaceChars(base::kWhitespaceASCII);
+
+std::vector<std::string> SplitText(const std::string& text,
+ const SkPaint& paint,
+ float max_width) {
+ std::vector<std::string> lines;
+ size_t first_char = 0;
+ while (first_char < text.size()) {
+ SkScalar width;
+ size_t char_break = first_char + paint.breakText(text.c_str() + first_char,
+ text.size() - first_char,
+ max_width, &width);
+ if (char_break == text.size()) {
+ lines.push_back(text.substr(first_char));
+ break;
+ }
+ size_t char_break_guess = char_break + 1;
+ while (char_break_guess > first_char &&
+ kWhitespaceChars.find(text[char_break_guess]) == std::string::npos)
+ char_break_guess--;
+
+ if (char_break_guess > first_char)
+ char_break = char_break_guess;
+
+ lines.push_back(text.substr(first_char, char_break - first_char));
+
+ first_char = char_break + (char_break_guess > first_char ? 1 : 0);
+ }
+ return lines;
+}
+
+} // namespace
+
+// TODO(acondor): Create non-square textures to reduce memory usage.
+UITexture::UITexture(int texture_handle, int texture_size)
+ : texture_handle_(texture_handle),
+ texture_size_(texture_size),
+ surface_(SkSurface::MakeRasterN32Premul(texture_size_, texture_size_)) {}
+
+UITexture::~UITexture() = default;
+
+void UITexture::DrawAndLayout() {
+ cc::SkiaPaintCanvas paint_canvas(surface_->getCanvas());
+ gfx::Canvas canvas(&paint_canvas, 1.0f);
+ canvas.DrawColor(0x00000000);
+ Draw(&canvas);
+ SetSize();
+}
+
+void UITexture::Flush() {
+ cc::SkiaPaintCanvas paint_canvas(surface_->getCanvas());
+ paint_canvas.flush();
+ SkPixmap pixmap;
+ CHECK(surface_->peekPixels(&pixmap));
+
+ glBindTexture(GL_TEXTURE_2D, texture_handle_);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr());
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+}
+
+void UITexture::DrawText(gfx::Canvas* canvas,
+ const std::string& text,
+ const cc::PaintFlags& flags,
+ float max_width,
+ bool v_centered) {
+ // TODO(acondor): Make use of gfx::Canvas::DrawStringRect instead.
+ // Platform specific font rendering capabilities need to be ported to Android.
+
+ const auto& paint = cc::ToSkPaint(flags);
+
+ auto lines = SplitText(text, paint, max_width);
+ if (lines.empty())
+ return;
+
+ float v_pos = flags.getTextSize();
+ if (v_centered) {
+ float height =
+ (lines.size() + (lines.size() - 1) * kDefaultLineSpacePercentage) *
+ flags.getTextSize();
+ v_pos -= height * .5;
+ }
+ for (const auto& line : lines) {
+ canvas->sk_canvas()->drawText(line.c_str(), line.size(), 0, v_pos, flags);
+ v_pos += flags.getTextSize() * (1 + kDefaultLineSpacePercentage);
+ }
+}
+
+} // namespace vr_shell

Powered by Google App Engine
This is Rietveld 408576698