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

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

Issue 12257016: (Not ready for review!) Toolbar and views high dpi support. Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Cleaned up more useless diffs. Created 7 years, 10 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 | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_paint_win.h » ('j') | 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 <limits> 7 #include <limits>
8 8
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "third_party/skia/include/core/SkBitmap.h" 11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/effects/SkGradientShader.h" 12 #include "third_party/skia/include/effects/SkGradientShader.h"
13 #include "ui/base/win/dpi.h"
13 #include "ui/gfx/canvas.h" 14 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font.h" 15 #include "ui/gfx/font.h"
15 #include "ui/gfx/rect.h" 16 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size_conversions.h" 17 #include "ui/gfx/size_conversions.h"
17 #include "ui/gfx/skia_util.h" 18 #include "ui/gfx/skia_util.h"
18 #include "ui/gfx/transform.h" 19 #include "ui/gfx/transform.h"
19 20
20 #if defined(OS_WIN) 21 #if defined(OS_WIN)
21 #include "ui/gfx/canvas_skia_paint.h" 22 #include "ui/gfx/canvas_skia_paint.h"
22 #endif 23 #endif
(...skipping 27 matching lines...) Expand all
50 owned_canvas_(skia::AdoptRef( 51 owned_canvas_(skia::AdoptRef(
51 skia::CreatePlatformCanvas(image_rep.pixel_width(), 52 skia::CreatePlatformCanvas(image_rep.pixel_width(),
52 image_rep.pixel_height(), 53 image_rep.pixel_height(),
53 is_opaque))), 54 is_opaque))),
54 canvas_(owned_canvas_.get()) { 55 canvas_(owned_canvas_.get()) {
55 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); 56 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_));
56 canvas_->scale(scale, scale); 57 canvas_->scale(scale, scale);
57 DrawImageInt(gfx::ImageSkia(image_rep), 0, 0); 58 DrawImageInt(gfx::ImageSkia(image_rep), 0, 0);
58 } 59 }
59 60
61 ui::ScaleFactor GetScaleFactor() {
62 #if defined(ENABLE_HIDPI)
63 float scale = ui::GetDPIScale();
64 if (scale > 1.6)
65 return ui::SCALE_FACTOR_180P;
66 else if (scale > 1.2)
67 return ui::SCALE_FACTOR_140P;
68 #endif
69 return ui::SCALE_FACTOR_100P;
70 }
71
60 Canvas::Canvas() 72 Canvas::Canvas()
61 : scale_factor_(ui::SCALE_FACTOR_100P), 73 : scale_factor_(GetScaleFactor()),
62 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))), 74 owned_canvas_(skia::AdoptRef(skia::CreatePlatformCanvas(0, 0, false))),
63 canvas_(owned_canvas_.get()) { 75 canvas_(owned_canvas_.get()) {
64 } 76 }
65 77
66 Canvas::~Canvas() { 78 Canvas::~Canvas() {
67 } 79 }
68 80
69 // static 81 // static
70 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas, 82 Canvas* Canvas::CreateCanvasWithoutScaling(SkCanvas* canvas,
71 ui::ScaleFactor scale_factor) { 83 ui::ScaleFactor scale_factor) {
72 return new Canvas(canvas, scale_factor); 84 return new Canvas(canvas, scale_factor);
73 } 85 }
74 86
75 void Canvas::RecreateBackingCanvas(const gfx::Size& size, 87 void Canvas::RecreateBackingCanvas(const gfx::Size& size,
76 ui::ScaleFactor scale_factor, 88 ui::ScaleFactor scale_factor,
77 bool is_opaque) { 89 bool is_opaque) {
78 scale_factor_ = scale_factor; 90 scale_factor_ = scale_factor;
79 gfx::Size pixel_size = gfx::ToFlooredSize( 91 gfx::Size pixel_size = gfx::ToFlooredSize(
80 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor))); 92 gfx::ScaleSize(size, ui::GetScaleFactorScale(scale_factor)));
81 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(), 93 owned_canvas_ = skia::AdoptRef(skia::CreatePlatformCanvas(pixel_size.width(),
82 pixel_size.height(), 94 pixel_size.height(),
83 is_opaque)); 95 is_opaque));
84 canvas_ = owned_canvas_.get(); 96 canvas_ = owned_canvas_.get();
85 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_)); 97 SkScalar scale = SkFloatToScalar(ui::GetScaleFactorScale(scale_factor_));
98 scale = 1;
86 canvas_->scale(scale, scale); 99 canvas_->scale(scale, scale);
87 } 100 }
88 101
89 // static 102 // static
90 int Canvas::GetStringWidth(const string16& text, const gfx::Font& font) { 103 int Canvas::GetStringWidth(const string16& text, const gfx::Font& font) {
91 int width = 0, height = 0; 104 int width = 0, height = 0;
92 Canvas::SizeStringInt(text, font, &width, &height, NO_ELLIPSIS); 105 Canvas::SizeStringInt(text, font, &width, &height, NO_ELLIPSIS);
93 return width; 106 return width;
94 } 107 }
95 108
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 SkRect out; 200 SkRect out;
188 bool has_non_empty_clip = canvas_->getClipBounds(&out); 201 bool has_non_empty_clip = canvas_->getClipBounds(&out);
189 bounds->SetRect(out.left(), out.top(), out.width(), out.height()); 202 bounds->SetRect(out.left(), out.top(), out.width(), out.height());
190 return has_non_empty_clip; 203 return has_non_empty_clip;
191 } 204 }
192 205
193 void Canvas::Translate(const gfx::Vector2d& offset) { 206 void Canvas::Translate(const gfx::Vector2d& offset) {
194 canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y())); 207 canvas_->translate(SkIntToScalar(offset.x()), SkIntToScalar(offset.y()));
195 } 208 }
196 209
210 #include "/gtools.h"
211 #if defined(ENABLE_HIDPI)
212 void Canvas::Scale(double x_scale, double y_scale) {
213 canvas_->scale(x_scale, y_scale);
214 // Debug(L"Scaling %p to %f", this, x_scale);
215 }
216 #else
197 void Canvas::Scale(int x_scale, int y_scale) { 217 void Canvas::Scale(int x_scale, int y_scale) {
198 canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale)); 218 canvas_->scale(SkIntToScalar(x_scale), SkIntToScalar(y_scale));
199 } 219 }
220 #endif
200 221
201 void Canvas::DrawColor(SkColor color) { 222 void Canvas::DrawColor(SkColor color) {
202 DrawColor(color, SkXfermode::kSrcOver_Mode); 223 DrawColor(color, SkXfermode::kSrcOver_Mode);
203 } 224 }
204 225
205 void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) { 226 void Canvas::DrawColor(SkColor color, SkXfermode::Mode mode) {
206 canvas_->drawColor(color, mode); 227 canvas_->drawColor(color, mode);
207 } 228 }
208 229
209 void Canvas::FillRect(const gfx::Rect& rect, SkColor color) { 230 void Canvas::FillRect(const gfx::Rect& rect, SkColor color) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 void Canvas::DrawImageInt(const gfx::ImageSkia& image, 320 void Canvas::DrawImageInt(const gfx::ImageSkia& image,
300 int x, int y, 321 int x, int y,
301 const SkPaint& paint) { 322 const SkPaint& paint) {
302 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image); 323 const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image);
303 if (image_rep.is_null()) 324 if (image_rep.is_null())
304 return; 325 return;
305 const SkBitmap& bitmap = image_rep.sk_bitmap(); 326 const SkBitmap& bitmap = image_rep.sk_bitmap();
306 float bitmap_scale = image_rep.GetScale(); 327 float bitmap_scale = image_rep.GetScale();
307 328
308 canvas_->save(); 329 canvas_->save();
309 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), 330 // canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
310 SkFloatToScalar(1.0f / bitmap_scale)); 331 // SkFloatToScalar(1.0f / bitmap_scale));
311 canvas_->drawBitmap(bitmap, 332 canvas_->drawBitmap(bitmap,
312 SkFloatToScalar(x * bitmap_scale), 333 SkFloatToScalar(x),// * bitmap_scale),
313 SkFloatToScalar(y * bitmap_scale), 334 SkFloatToScalar(y),// * bitmap_scale),
314 &paint); 335 &paint);
315 canvas_->restore(); 336 canvas_->restore();
316 } 337 }
317 338
318 void Canvas::DrawImageInt(const gfx::ImageSkia& image, 339 void Canvas::DrawImageInt(const gfx::ImageSkia& image,
319 int src_x, int src_y, int src_w, int src_h, 340 int src_x, int src_y, int src_w, int src_h,
320 int dest_x, int dest_y, int dest_w, int dest_h, 341 int dest_x, int dest_y, int dest_w, int dest_h,
321 bool filter) { 342 bool filter) {
322 SkPaint p; 343 SkPaint p;
323 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, 344 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y,
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 554
534 float bitmap_scale = image_rep.GetScale(); 555 float bitmap_scale = image_rep.GetScale();
535 if (scale_x < bitmap_scale || scale_y < bitmap_scale) 556 if (scale_x < bitmap_scale || scale_y < bitmap_scale)
536 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); 557 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap();
537 } 558 }
538 559
539 return image_rep; 560 return image_rep;
540 } 561 }
541 562
542 } // namespace gfx 563 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/canvas.h ('k') | ui/gfx/canvas_paint_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698