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

Side by Side Diff: ui/gfx/render_text.cc

Issue 1842693002: Avoid applying alpha twice in RenderText. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cherry pick the correct patch... Created 4 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <climits> 10 #include <climits>
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 colors->push_back(c1); 137 colors->push_back(c1);
138 } 138 }
139 139
140 // Creates a SkShader to fade the text, with |left_part| specifying the left 140 // Creates a SkShader to fade the text, with |left_part| specifying the left
141 // fade effect, if any, and |right_part| specifying the right fade effect. 141 // fade effect, if any, and |right_part| specifying the right fade effect.
142 skia::RefPtr<SkShader> CreateFadeShader(const FontList& font_list, 142 skia::RefPtr<SkShader> CreateFadeShader(const FontList& font_list,
143 const Rect& text_rect, 143 const Rect& text_rect,
144 const Rect& left_part, 144 const Rect& left_part,
145 const Rect& right_part, 145 const Rect& right_part,
146 SkColor color) { 146 SkColor color) {
147 // The shader should only specify transparency of the fade itself, not the
148 // original transparency, which will be applied by the actual renderer.
149 DCHECK_EQ(SkColorGetA(color), static_cast<uint8_t>(0xff));
150
147 // In general, fade down to 0 alpha. But when the available width is less 151 // In general, fade down to 0 alpha. But when the available width is less
148 // than four characters, linearly ramp up the fade target alpha to as high as 152 // than four characters, linearly ramp up the fade target alpha to as high as
149 // 20% at zero width. This allows the user to see the last faded characters a 153 // 20% at zero width. This allows the user to see the last faded characters a
150 // little better when there are only a few characters shown. 154 // little better when there are only a few characters shown.
151 const float width_fraction = 155 const float width_fraction =
152 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4)); 156 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4));
153 const SkAlpha kAlphaAtZeroWidth = 51; 157 const SkAlpha kAlphaAtZeroWidth = 51;
154 const SkAlpha alpha = (width_fraction < 1) ? 158 const SkAlpha alpha = (width_fraction < 1) ?
155 static_cast<SkAlpha>(round((1 - width_fraction) * kAlphaAtZeroWidth)) : 0; 159 static_cast<SkAlpha>(round((1 - width_fraction) * kAlphaAtZeroWidth)) : 0;
156 const SkColor fade_color = SkColorSetA(color, alpha); 160 const SkColor fade_color = SkColorSetA(color, alpha);
(...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); 1230 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0);
1227 solid_part.Inset(0, 0, gradient_width, 0); 1231 solid_part.Inset(0, 0, gradient_width, 0);
1228 } 1232 }
1229 1233
1230 Rect text_rect = display_rect(); 1234 Rect text_rect = display_rect();
1231 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); 1235 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0);
1232 1236
1233 // TODO(msw): Use the actual text colors corresponding to each faded part. 1237 // TODO(msw): Use the actual text colors corresponding to each faded part.
1234 skia::RefPtr<SkShader> shader = 1238 skia::RefPtr<SkShader> shader =
1235 CreateFadeShader(font_list(), text_rect, left_part, right_part, 1239 CreateFadeShader(font_list(), text_rect, left_part, right_part,
1236 colors_.breaks().front().second); 1240 SkColorSetA(colors_.breaks().front().second, 0xff));
1237 if (shader) 1241 if (shader)
1238 renderer->SetShader(shader.get()); 1242 renderer->SetShader(shader.get());
1239 } 1243 }
1240 1244
1241 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { 1245 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
1242 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); 1246 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_);
1243 renderer->SetDrawLooper(looper.get()); 1247 renderer->SetDrawLooper(looper.get());
1244 } 1248 }
1245 1249
1246 base::i18n::TextDirection RenderText::GetTextDirection( 1250 base::i18n::TextDirection RenderText::GetTextDirection(
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1544 1548
1545 SetDisplayOffset(display_offset_.x() + delta_x); 1549 SetDisplayOffset(display_offset_.x() + delta_x);
1546 } 1550 }
1547 1551
1548 void RenderText::DrawSelection(Canvas* canvas) { 1552 void RenderText::DrawSelection(Canvas* canvas) {
1549 for (const Rect& s : GetSubstringBounds(selection())) 1553 for (const Rect& s : GetSubstringBounds(selection()))
1550 canvas->FillRect(s, selection_background_focused_color_); 1554 canvas->FillRect(s, selection_background_focused_color_);
1551 } 1555 }
1552 1556
1553 } // namespace gfx 1557 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698