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

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

Issue 2863453002: ui: Change DrawDashedRect to use a dash effect instead of bitmap. (Closed)
Patch Set: asd 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
« 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/canvas.h" 5 #include "ui/gfx/canvas.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "cc/paint/paint_flags.h" 12 #include "cc/paint/paint_flags.h"
13 #include "cc/paint/paint_shader.h" 13 #include "cc/paint/paint_shader.h"
14 #include "third_party/skia/include/core/SkBitmap.h" 14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "third_party/skia/include/core/SkPath.h" 15 #include "third_party/skia/include/core/SkPath.h"
16 #include "third_party/skia/include/core/SkRefCnt.h" 16 #include "third_party/skia/include/core/SkRefCnt.h"
17 #include "third_party/skia/include/effects/SkDashPathEffect.h"
17 #include "third_party/skia/include/effects/SkGradientShader.h" 18 #include "third_party/skia/include/effects/SkGradientShader.h"
18 #include "ui/gfx/font_list.h" 19 #include "ui/gfx/font_list.h"
19 #include "ui/gfx/geometry/insets_f.h" 20 #include "ui/gfx/geometry/insets_f.h"
20 #include "ui/gfx/geometry/rect.h" 21 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/geometry/rect_conversions.h" 22 #include "ui/gfx/geometry/rect_conversions.h"
22 #include "ui/gfx/geometry/rect_f.h" 23 #include "ui/gfx/geometry/rect_f.h"
23 #include "ui/gfx/geometry/safe_integer_conversions.h" 24 #include "ui/gfx/geometry/safe_integer_conversions.h"
24 #include "ui/gfx/geometry/size_conversions.h" 25 #include "ui/gfx/geometry/size_conversions.h"
25 #include "ui/gfx/scoped_canvas.h" 26 #include "ui/gfx/scoped_canvas.h"
26 #include "ui/gfx/skia_paint_util.h" 27 #include "ui/gfx/skia_paint_util.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 float width = 0, height = 0; 97 float width = 0, height = 0;
97 SizeStringFloat(text, font_list, &width, &height, 0, NO_ELLIPSIS); 98 SizeStringFloat(text, font_list, &width, &height, 0, NO_ELLIPSIS);
98 return width; 99 return width;
99 } 100 }
100 101
101 // static 102 // static
102 int Canvas::DefaultCanvasTextAlignment() { 103 int Canvas::DefaultCanvasTextAlignment() {
103 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; 104 return base::i18n::IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT;
104 } 105 }
105 106
106 void Canvas::DrawDashedRect(const RectF& rect, SkColor color) { 107 void Canvas::DrawDashedRect(const RectF& inrect, SkColor color) {
107 if (rect.IsEmpty()) 108 if (inrect.IsEmpty())
108 return; 109 return;
109 // Create a 2D bitmap containing alternating on/off pixels - we do this 110 RectF rect = inrect;
110 // so that you never get two pixels of the same color around the edges
111 // of the focus rect (this may mean that opposing edges of the rect may
112 // have a dot pattern out of phase to each other).
113 static SkColor last_color;
114 static SkBitmap* dots = NULL;
115 if (!dots || last_color != color) {
116 int col_pixels = 32;
117 int row_pixels = 32;
118 111
119 delete dots; 112 cc::PaintFlags flags;
120 last_color = color; 113 flags.setColor(color);
121 dots = new SkBitmap; 114 SkScalar intervals[] = {1.f, 1.f};
122 dots->allocN32Pixels(col_pixels, row_pixels); 115 flags.setStrokeWidth(1.f);
123 dots->eraseARGB(0, 0, 0, 0); 116 flags.setStyle(cc::PaintFlags::kStroke_Style);
117 rect.Inset(gfx::InsetsF(0.5f));
124 118
125 uint32_t* dot = dots->getAddr32(0, 0); 119 flags.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
126 for (int i = 0; i < row_pixels; i++) {
127 for (int u = 0; u < col_pixels; u++) {
128 if ((u % 2 + i % 2) % 2 != 0) {
129 dot[i * row_pixels + u] = color;
130 }
131 }
132 }
133 }
134 120
135 // Make a shader for the bitmap with an origin of the box we'll draw. 121 // Top-left to top-right.
136 cc::PaintFlags flags; 122 canvas_->drawLine(rect.x() - 0.5f, rect.y(), rect.right() + 0.5f, rect.y(),
137 flags.setShader(cc::WrapSkShader(SkShader::MakeBitmapShader( 123 flags);
138 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode))); 124 // Top-left to bottom-left.
139 125 canvas_->drawLine(rect.right() + 0.5f, rect.bottom(), rect.x() - 0.5f,
140 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), flags); 126 rect.bottom(), flags);
141 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), 127 // Bottom-right to bottom-left.
142 flags); 128 canvas_->drawLine(rect.x(), rect.y() - 0.5f, rect.x(), rect.bottom() + 0.5f,
143 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), flags); 129 flags);
144 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), 130 // Bottom-right to top-right.
145 flags); 131 canvas_->drawLine(rect.right(), rect.bottom() + 0.5f, rect.right(),
132 rect.y() - 0.5f, flags);
146 } 133 }
147 134
148 float Canvas::UndoDeviceScaleFactor() { 135 float Canvas::UndoDeviceScaleFactor() {
149 SkScalar scale_factor = 1.0f / image_scale_; 136 SkScalar scale_factor = 1.0f / image_scale_;
150 canvas_->scale(scale_factor, scale_factor); 137 canvas_->scale(scale_factor, scale_factor);
151 return image_scale_; 138 return image_scale_;
152 } 139 }
153 140
154 void Canvas::Save() { 141 void Canvas::Save() {
155 canvas_->save(); 142 canvas_->save();
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 bitmap_.emplace(); 569 bitmap_.emplace();
583 bitmap_->allocPixels(info); 570 bitmap_->allocPixels(info);
584 // Ensure that the bitmap is zeroed, since the code expects that. 571 // Ensure that the bitmap is zeroed, since the code expects that.
585 memset(bitmap_->getPixels(), 0, bitmap_->getSafeSize()); 572 memset(bitmap_->getPixels(), 0, bitmap_->getSafeSize());
586 573
587 owned_canvas_.emplace(bitmap_.value()); 574 owned_canvas_.emplace(bitmap_.value());
588 return &owned_canvas_.value(); 575 return &owned_canvas_.value();
589 } 576 }
590 577
591 } // namespace gfx 578 } // 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