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

Side by Side Diff: chrome/browser/android/vr_shell/textures/ui_texture.cc

Issue 2877673005: VR: Avoiding regeneration of RenderText objects for texture rendering (Closed)
Patch Set: Created 3 years, 7 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 unified diff | Download patch
OLDNEW
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/textures/ui_texture.h" 5 #include "chrome/browser/android/vr_shell/textures/ui_texture.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/i18n/char_iterator.h" 11 #include "base/i18n/char_iterator.h"
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/trace_event/trace_event.h"
15 #include "chrome/browser/android/vr_shell/font_fallback.h" 16 #include "chrome/browser/android/vr_shell/font_fallback.h"
16 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
17 #include "third_party/icu/source/common/unicode/uscript.h" 18 #include "third_party/icu/source/common/unicode/uscript.h"
18 #include "third_party/skia/include/core/SkCanvas.h" 19 #include "third_party/skia/include/core/SkCanvas.h"
19 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/font_list.h" 21 #include "ui/gfx/font_list.h"
22 #include "ui/gfx/render_text.h"
23 #include "ui/gfx/text_elider.h"
21 #include "ui/gl/gl_bindings.h" 24 #include "ui/gl/gl_bindings.h"
22 25
23 namespace vr_shell { 26 namespace vr_shell {
24 27
25 namespace { 28 namespace {
26 29
27 static constexpr char kDefaultFontFamily[] = "sans-serif"; 30 static constexpr char kDefaultFontFamily[] = "sans-serif";
28 31
32 std::unique_ptr<gfx::RenderText> CreateRenderText(
33 const base::string16& text,
34 const gfx::FontList& font_list,
35 SkColor color,
36 int flags) {
37 std::unique_ptr<gfx::RenderText> render_text(
38 gfx::RenderText::CreateInstance());
39 render_text->SetText(text);
40 render_text->SetFontList(font_list);
41 render_text->SetColor(color);
42
43 if (flags & UiTexture::TEXT_ALIGN_LEFT)
cjgrant 2017/05/11 21:15:40 It looks like RenderText defaults to either RIGHT
acondor_ 2017/05/11 22:26:49 Good catch. I'm removing the flag setup in Prepare
44 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT);
45 else if (flags & UiTexture::TEXT_ALIGN_RIGHT)
46 render_text->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
47 else
48 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER);
49
50 const int font_style = font_list.GetFontStyle();
51 render_text->SetStyle(gfx::ITALIC, (font_style & gfx::Font::ITALIC) != 0);
52 render_text->SetStyle(gfx::UNDERLINE,
53 (font_style & gfx::Font::UNDERLINE) != 0);
54 render_text->SetWeight(font_list.GetFontWeight());
55
56 return render_text;
57 }
58
29 std::set<UChar32> CollectDifferentChars(base::string16 text) { 59 std::set<UChar32> CollectDifferentChars(base::string16 text) {
30 std::set<UChar32> characters; 60 std::set<UChar32> characters;
31 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { 61 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) {
32 characters.insert(it.get()); 62 characters.insert(it.get());
33 } 63 }
34 return characters; 64 return characters;
35 } 65 }
36 66
37 } // namespace 67 } // namespace
38 68
39 UiTexture::UiTexture() = default; 69 UiTexture::UiTexture() = default;
40 70
41 UiTexture::~UiTexture() = default; 71 UiTexture::~UiTexture() = default;
42 72
43 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { 73 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) {
74 TRACE_EVENT0("gpu", "UiTexture::DrawAndLayout");
44 canvas->drawColor(SK_ColorTRANSPARENT); 75 canvas->drawColor(SK_ColorTRANSPARENT);
45 Draw(canvas, texture_size); 76 Draw(canvas, texture_size);
46 } 77 }
47 78
79 std::vector<std::unique_ptr<gfx::RenderText>> UiTexture::PrepareDrawStringRect(
80 const base::string16& text,
81 const gfx::FontList& font_list,
82 SkColor color,
83 gfx::Rect* bounds,
84 int flags) {
85 DCHECK(bounds);
86
87 std::vector<std::unique_ptr<gfx::RenderText>> lines;
88 gfx::Rect rect(*bounds);
89
90 if (!(flags & (TEXT_ALIGN_LEFT | TEXT_ALIGN_CENTER | TEXT_ALIGN_RIGHT))) {
91 flags |= IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT;
92 }
93
94 if (flags & MULTI_LINE) {
95 std::vector<base::string16> strings;
96 gfx::ElideRectangleText(text, font_list, bounds->width(),
97 bounds->height() ? bounds->height() : INT_MAX,
98 gfx::ELIDE_LONG_WORDS, &strings);
mthiesse 2017/05/12 01:14:38 Probably want WRAP_LONG_WORDS here, to align with
acondor_ 2017/05/12 14:54:23 Done.
99
100 int height = 0;
101 int line_height = 0;
102 for (size_t i = 0; i < strings.size(); i++) {
103 std::unique_ptr<gfx::RenderText> render_text =
104 CreateRenderText(strings[i], font_list, color, flags);
105
106 if (i == 0) {
107 // Measure line and center text vertically.
108 line_height = render_text->GetStringSize().height();
109 rect.set_height(line_height);
110 if (bounds->height()) {
111 const int text_height = strings.size() * line_height;
112 rect += gfx::Vector2d(0, (bounds->height() - text_height) / 2);
113 }
114 }
115
116 render_text->SetDisplayRect(rect);
117 height += line_height;
118 rect += gfx::Vector2d(0, line_height);
119 lines.push_back(std::move(render_text));
120 }
121
122 // Set calculated height.
123 if (bounds->height() == 0)
124 bounds->set_height(height);
125
126 } else {
127 std::unique_ptr<gfx::RenderText> render_text =
128 CreateRenderText(text, font_list, color, flags);
129 if (bounds->width() != 0)
130 render_text->SetElideBehavior(gfx::FADE_TAIL);
mthiesse 2017/05/12 01:14:38 Pretty sure we want gfx::TRUNCATE per UX specs.
acondor_ 2017/05/12 14:54:23 Done.
131 else
132 rect.set_width(INT_MAX);
133
134 render_text->SetDisplayRect(rect);
135
136 if (bounds->width() == 0) {
137 int text_width = render_text->GetStringSize().width();
138 bounds->set_width(text_width);
139 }
140
141 lines.push_back(std::move(render_text));
142 }
143 return lines;
144 }
145
48 bool UiTexture::IsRTL() { 146 bool UiTexture::IsRTL() {
49 return base::i18n::IsRTL(); 147 return base::i18n::IsRTL();
50 } 148 }
51 149
52 gfx::FontList UiTexture::GetDefaultFontList(int size) { 150 gfx::FontList UiTexture::GetDefaultFontList(int size) {
53 return gfx::FontList(gfx::Font(kDefaultFontFamily, size)); 151 return gfx::FontList(gfx::Font(kDefaultFontFamily, size));
54 } 152 }
55 153
56 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { 154 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) {
57 gfx::Font default_font(kDefaultFontFamily, size); 155 gfx::Font default_font(kDefaultFontFamily, size);
58 std::vector<gfx::Font> fonts{default_font}; 156 std::vector<gfx::Font> fonts{default_font};
59 157
60 std::set<std::string> names; 158 std::set<std::string> names;
61 // TODO(acondor): Query BrowserProcess to obtain the application locale. 159 // TODO(acondor): Query BrowserProcess to obtain the application locale.
62 for (UChar32 c : CollectDifferentChars(text)) { 160 for (UChar32 c : CollectDifferentChars(text)) {
63 std::string name = GetFallbackFontNameForChar(default_font, c, ""); 161 std::string name = GetFallbackFontNameForChar(default_font, c, "");
64 if (!name.empty()) 162 if (!name.empty())
65 names.insert(name); 163 names.insert(name);
66 } 164 }
67 for (const auto& name : names) 165 for (const auto& name : names)
68 fonts.push_back(gfx::Font(name, size)); 166 fonts.push_back(gfx::Font(name, size));
69 return gfx::FontList(fonts); 167 return gfx::FontList(fonts);
70 } 168 }
71 169
72 } // namespace vr_shell 170 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698