Commit 08d5e289 authored by Christopher Grant's avatar Christopher Grant Committed by Commit Bot

VR: Revert splitting of Text and TextTexture

After additional discussion, it was decided that the texture should
remain a private implementation detail rather than exposed class. This
keeps the UI on track to have element render themselves directly, rather
than through separate classes.

BUG=
R=vollick

Cq-Include-Trybots: master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: Iee191543884cda51605356da7215ec3c036c5734
Reviewed-on: https://chromium-review.googlesource.com/806575Reviewed-by: default avatarBiao She <bshe@chromium.org>
Commit-Queue: Christopher Grant <cjgrant@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521460}
parent 5d627d2e
...@@ -84,8 +84,6 @@ static_library("vr_common") { ...@@ -84,8 +84,6 @@ static_library("vr_common") {
"elements/text.h", "elements/text.h",
"elements/text_input.cc", "elements/text_input.cc",
"elements/text_input.h", "elements/text_input.h",
"elements/text_texture.cc",
"elements/text_texture.h",
"elements/textured_element.cc", "elements/textured_element.cc",
"elements/textured_element.h", "elements/textured_element.h",
"elements/throbber.cc", "elements/throbber.cc",
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "cc/paint/skia_paint_canvas.h" #include "cc/paint/skia_paint_canvas.h"
#include "chrome/browser/vr/elements/text_texture.h" #include "chrome/browser/vr/elements/ui_texture.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
...@@ -15,6 +15,51 @@ ...@@ -15,6 +15,51 @@
namespace vr { namespace vr {
class TextTexture : public UiTexture {
public:
explicit TextTexture(float font_height) : font_height_(font_height) {}
~TextTexture() override {}
void SetText(const base::string16& text) { SetAndDirty(&text_, text); }
void SetColor(SkColor color) { SetAndDirty(&color_, color); }
void SetAlignment(TextAlignment alignment) {
SetAndDirty(&alignment_, alignment);
}
void SetMultiLine(bool multiline) { SetAndDirty(&multiline_, multiline); }
void SetTextWidth(float width) { SetAndDirty(&text_width_, width); }
gfx::SizeF GetDrawnSize() const override { return size_; }
// This method does all text preparation for the element other than drawing to
// the texture. This allows for deeper unit testing of the Text element
// without having to mock canvases and simulate frame rendering. The state of
// the texture is modified here.
std::vector<std::unique_ptr<gfx::RenderText>> LayOutText(
const gfx::Size& texture_size);
private:
gfx::Size GetPreferredTextureSize(int width) const override {
return gfx::Size(width, width);
}
void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override;
gfx::SizeF size_;
base::string16 text_;
// These dimensions are in meters.
float font_height_ = 0;
float text_width_ = 0;
TextAlignment alignment_ = kTextAlignmentCenter;
bool multiline_ = true;
SkColor color_ = SK_ColorBLACK;
DISALLOW_COPY_AND_ASSIGN(TextTexture);
};
Text::Text(int maximum_width_pixels, float font_height_meters) Text::Text(int maximum_width_pixels, float font_height_meters)
: TexturedElement(maximum_width_pixels), : TexturedElement(maximum_width_pixels),
texture_(base::MakeUnique<TextTexture>(font_height_meters)) {} texture_(base::MakeUnique<TextTexture>(font_height_meters)) {}
...@@ -40,12 +85,48 @@ void Text::OnSetSize(gfx::SizeF size) { ...@@ -40,12 +85,48 @@ void Text::OnSetSize(gfx::SizeF size) {
texture_->SetTextWidth(size.width()); texture_->SetTextWidth(size.width());
} }
TextTexture* Text::GetTextureForTest() { std::vector<std::unique_ptr<gfx::RenderText>> Text::LayOutTextForTest(
return texture_.get(); const gfx::Size& texture_size) {
return texture_->LayOutText(texture_size);
}
gfx::SizeF Text::GetTextureSizeForTest() const {
return texture_->GetDrawnSize();
} }
UiTexture* Text::GetTexture() const { UiTexture* Text::GetTexture() const {
return texture_.get(); return texture_.get();
} }
std::vector<std::unique_ptr<gfx::RenderText>> TextTexture::LayOutText(
const gfx::Size& texture_size) {
gfx::FontList fonts;
float pixels_per_meter = texture_size.width() / text_width_;
int pixel_font_height = static_cast<int>(font_height_ * pixels_per_meter);
GetDefaultFontList(pixel_font_height, text_, &fonts);
gfx::Rect text_bounds(texture_size.width(), 0);
std::vector<std::unique_ptr<gfx::RenderText>> lines =
// TODO(vollick): if this subsumes all text, then we should probably move
// this function into this class.
PrepareDrawStringRect(
text_, fonts, color_, &text_bounds, alignment_,
multiline_ ? kWrappingBehaviorWrap : kWrappingBehaviorNoWrap);
// Note, there is no padding here whatsoever.
size_ = gfx::SizeF(text_bounds.size());
return lines;
}
void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) {
cc::SkiaPaintCanvas paint_canvas(sk_canvas);
gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
gfx::Canvas* canvas = &gfx_canvas;
auto lines = LayOutText(texture_size);
for (auto& render_text : lines)
render_text->Draw(canvas);
}
} // namespace vr } // namespace vr
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
#include "chrome/browser/vr/elements/ui_texture.h" #include "chrome/browser/vr/elements/ui_texture.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
namespace gfx {
class RenderText;
}
namespace vr { namespace vr {
class TextTexture; class TextTexture;
...@@ -22,12 +26,15 @@ class Text : public TexturedElement { ...@@ -22,12 +26,15 @@ class Text : public TexturedElement {
void SetText(const base::string16& text); void SetText(const base::string16& text);
void SetColor(SkColor color); void SetColor(SkColor color);
void SetTextAlignment(UiTexture::TextAlignment alignment); void SetTextAlignment(UiTexture::TextAlignment alignment);
void SetMultiLine(bool multiline); void SetMultiLine(bool multiline);
void OnSetSize(gfx::SizeF size) override; void OnSetSize(gfx::SizeF size) override;
TextTexture* GetTextureForTest(); std::vector<std::unique_ptr<gfx::RenderText>> LayOutTextForTest(
const gfx::Size& texture_size);
gfx::SizeF GetTextureSizeForTest() const;
private: private:
UiTexture* GetTexture() const override; UiTexture* GetTexture() const override;
......
// 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/vr/elements/text_texture.h"
#include "base/memory/ptr_util.h"
#include "cc/paint/skia_paint_canvas.h"
#include "chrome/browser/vr/elements/ui_texture.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/render_text.h"
namespace vr {
TextTexture::TextTexture(float font_height) : font_height_(font_height) {}
TextTexture::~TextTexture() {}
void TextTexture::SetText(const base::string16& text) {
SetAndDirty(&text_, text);
}
void TextTexture::SetColor(SkColor color) {
SetAndDirty(&color_, color);
}
void TextTexture::SetAlignment(TextAlignment alignment) {
SetAndDirty(&alignment_, alignment);
}
void TextTexture::SetMultiLine(bool multiline) {
SetAndDirty(&multiline_, multiline);
}
void TextTexture::SetTextWidth(float width) {
SetAndDirty(&text_width_, width);
}
gfx::SizeF TextTexture::GetDrawnSize() const {
return size_;
}
std::vector<std::unique_ptr<gfx::RenderText>> TextTexture::LayOutText(
const gfx::Size& texture_size) {
gfx::FontList fonts;
float pixels_per_meter = texture_size.width() / text_width_;
int pixel_font_height = static_cast<int>(font_height_ * pixels_per_meter);
GetDefaultFontList(pixel_font_height, text_, &fonts);
gfx::Rect text_bounds(texture_size.width(), 0);
std::vector<std::unique_ptr<gfx::RenderText>> lines =
// TODO(vollick): if this subsumes all text, then we should probably move
// this function into this class.
PrepareDrawStringRect(
text_, fonts, color_, &text_bounds, alignment_,
multiline_ ? kWrappingBehaviorWrap : kWrappingBehaviorNoWrap);
// Note, there is no padding here whatsoever.
size_ = gfx::SizeF(text_bounds.size());
return lines;
}
gfx::Size TextTexture::GetPreferredTextureSize(int width) const {
return gfx::Size(width, width);
}
void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) {
cc::SkiaPaintCanvas paint_canvas(sk_canvas);
gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
gfx::Canvas* canvas = &gfx_canvas;
auto lines = LayOutText(texture_size);
for (auto& render_text : lines)
render_text->Draw(canvas);
}
} // namespace vr
// 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.
#ifndef CHROME_BROWSER_VR_ELEMENTS_TEXT_TEXTURE_H_
#define CHROME_BROWSER_VR_ELEMENTS_TEXT_TEXTURE_H_
#include <memory>
#include "chrome/browser/vr/elements/textured_element.h"
#include "chrome/browser/vr/elements/ui_texture.h"
#include "third_party/skia/include/core/SkColor.h"
namespace gfx {
class RenderText;
}
namespace vr {
class TextTexture : public UiTexture {
public:
explicit TextTexture(float font_height);
~TextTexture() override;
void SetText(const base::string16& text);
void SetColor(SkColor color);
void SetAlignment(TextAlignment alignment);
void SetMultiLine(bool multiline);
void SetTextWidth(float width);
gfx::SizeF GetDrawnSize() const override;
// This method does all text preparation for the element other than drawing to
// the texture. This allows for deeper unit testing of the Text element
// without having to mock canvases and simulate frame rendering. The state of
// the texture is modified here.
std::vector<std::unique_ptr<gfx::RenderText>> LayOutText(
const gfx::Size& texture_size);
private:
gfx::Size GetPreferredTextureSize(int width) const override;
void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override;
gfx::SizeF size_;
base::string16 text_;
// These dimensions are in meters.
float font_height_ = 0;
float text_width_ = 0;
TextAlignment alignment_ = kTextAlignmentCenter;
bool multiline_ = true;
SkColor color_ = SK_ColorBLACK;
DISALLOW_COPY_AND_ASSIGN(TextTexture);
};
} // namespace vr
#endif // CHROME_BROWSER_VR_ELEMENTS_TEXT_TEXTURE_H_
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/vr/elements/text_texture.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/render_text.h" #include "ui/gfx/render_text.h"
...@@ -21,27 +20,25 @@ TEST(Text, MultiLine) { ...@@ -21,27 +20,25 @@ TEST(Text, MultiLine) {
text->SetSize(kInitialSize, 0); text->SetSize(kInitialSize, 0);
text->SetText(base::UTF8ToUTF16(std::string(1000, 'x'))); text->SetText(base::UTF8ToUTF16(std::string(1000, 'x')));
TextTexture* texture = text->GetTextureForTest();
// Make sure we get multiple lines of rendered text from the string. // Make sure we get multiple lines of rendered text from the string.
auto layout = texture->LayOutText(texture_size); auto layout = text->LayOutTextForTest(texture_size);
size_t initial_num_lines = layout.size(); size_t initial_num_lines = layout.size();
auto initial_size = texture->GetDrawnSize(); auto initial_size = text->GetTextureSizeForTest();
EXPECT_GT(initial_num_lines, 1u); EXPECT_GT(initial_num_lines, 1u);
EXPECT_GT(initial_size.height(), 0.f); EXPECT_GT(initial_size.height(), 0.f);
// Reduce the field width, and ensure that the number of lines increases along // Reduce the field width, and ensure that the number of lines increases along
// with the texture height. // with the texture height.
text->SetSize(kInitialSize / 2, 0); text->SetSize(kInitialSize / 2, 0);
layout = texture->LayOutText(texture_size); layout = text->LayOutTextForTest(texture_size);
EXPECT_GT(layout.size(), initial_num_lines); EXPECT_GT(layout.size(), initial_num_lines);
EXPECT_GT(texture->GetDrawnSize().height(), initial_size.height()); EXPECT_GT(text->GetTextureSizeForTest().height(), initial_size.height());
// Enforce single-line rendering. // Enforce single-line rendering.
text->SetMultiLine(false); text->SetMultiLine(false);
layout = texture->LayOutText(texture_size); layout = text->LayOutTextForTest(texture_size);
EXPECT_EQ(layout.size(), 1u); EXPECT_EQ(layout.size(), 1u);
EXPECT_LT(texture->GetDrawnSize().height(), initial_size.height()); EXPECT_LT(text->GetTextureSizeForTest().height(), initial_size.height());
} }
} // namespace vr } // namespace vr
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment