Commit da76437a authored by danakj's avatar danakj Committed by Commit bot

gfx: Rename canvas SaveAndUnscale to UnscaleTheDeviceScaleFactor.

This changes the method to only do the unscaling. Callers use a
ScopedCanvas instead. Then they don't need to call Restore()
themselves.

R=pkasting, sky

Review URL: https://codereview.chromium.org/1425163002

Cr-Commit-Position: refs/heads/master@{#357172}
parent 591eec3d
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/skia_util.h" #include "ui/gfx/skia_util.h"
#include "ui/views/view.h" #include "ui/views/view.h"
#include "ui/views/widget/native_widget_aura.h" #include "ui/views/widget/native_widget_aura.h"
...@@ -262,13 +263,13 @@ void DefaultHeaderPainter::PaintTitleBar(gfx::Canvas* canvas) { ...@@ -262,13 +263,13 @@ void DefaultHeaderPainter::PaintTitleBar(gfx::Canvas* canvas) {
} }
void DefaultHeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) { void DefaultHeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) {
const float scale = canvas->SaveAndUnscale(); gfx::ScopedCanvas scoped_canvas(canvas);
const float scale = canvas->UndoDeviceScaleFactor();
gfx::RectF rect(0, painted_height_ * scale - 1, view_->width() * scale, 1); gfx::RectF rect(0, painted_height_ * scale - 1, view_->width() * scale, 1);
SkPaint paint; SkPaint paint;
paint.setColor((mode_ == MODE_ACTIVE) ? paint.setColor((mode_ == MODE_ACTIVE) ?
kHeaderContentSeparatorColor : kHeaderContentSeparatorInactiveColor); kHeaderContentSeparatorColor : kHeaderContentSeparatorInactiveColor);
canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), paint); canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), paint);
canvas->Restore();
} }
void DefaultHeaderPainter::LayoutLeftHeaderView() { void DefaultHeaderPainter::LayoutLeftHeaderView() {
......
...@@ -83,6 +83,7 @@ ...@@ -83,6 +83,7 @@
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/favicon_size.h" #include "ui/gfx/favicon_size.h"
#include "ui/gfx/paint_vector_icon.h" #include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/text_constants.h" #include "ui/gfx/text_constants.h"
#include "ui/gfx/text_elider.h" #include "ui/gfx/text_elider.h"
#include "ui/gfx/vector_icons_public.h" #include "ui/gfx/vector_icons_public.h"
...@@ -478,10 +479,11 @@ class BookmarkBarView::ButtonSeparatorView : public views::View { ...@@ -478,10 +479,11 @@ class BookmarkBarView::ButtonSeparatorView : public views::View {
void OnPaint(gfx::Canvas* canvas) override { void OnPaint(gfx::Canvas* canvas) override {
if (ui::MaterialDesignController::IsModeMaterial()) { if (ui::MaterialDesignController::IsModeMaterial()) {
gfx::ScopedCanvas scoped_canvas(canvas);
// 1px wide at all scale factors. If there is an uneven amount of padding // 1px wide at all scale factors. If there is an uneven amount of padding
// left over, place the extra pixel on the outside, i.e. away from the // left over, place the extra pixel on the outside, i.e. away from the
// "Other bookmarks" folder. // "Other bookmarks" folder.
const float scale = canvas->SaveAndUnscale(); const float scale = canvas->UndoDeviceScaleFactor();
const gfx::RectF scaled_bounds = const gfx::RectF scaled_bounds =
gfx::ScaleRect(gfx::RectF(bounds()), scale); gfx::ScaleRect(gfx::RectF(bounds()), scale);
...@@ -497,7 +499,6 @@ class BookmarkBarView::ButtonSeparatorView : public views::View { ...@@ -497,7 +499,6 @@ class BookmarkBarView::ButtonSeparatorView : public views::View {
SkColorSetA(GetThemeProvider()->GetColor( SkColorSetA(GetThemeProvider()->GetColor(
ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON), ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON),
0x4D)); 0x4D));
canvas->Restore();
} else { } else {
PaintVerticalDivider( PaintVerticalDivider(
canvas, kSeparatorStartX, height(), 1, kEdgeDividerColor, canvas, kSeparatorStartX, height(), 1, kEdgeDividerColor,
......
...@@ -134,6 +134,7 @@ ...@@ -134,6 +134,7 @@
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/color_utils.h" #include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/rect_conversions.h" #include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/screen.h" #include "ui/gfx/screen.h"
#include "ui/strings/grit/ui_strings.h" #include "ui/strings/grit/ui_strings.h"
#include "ui/views/controls/button/menu_button.h" #include "ui/views/controls/button/menu_button.h"
...@@ -551,13 +552,13 @@ BrowserView* BrowserView::GetBrowserViewForBrowser(const Browser* browser) { ...@@ -551,13 +552,13 @@ BrowserView* BrowserView::GetBrowserViewForBrowser(const Browser* browser) {
void BrowserView::Paint1pxHorizontalLine(gfx::Canvas* canvas, void BrowserView::Paint1pxHorizontalLine(gfx::Canvas* canvas,
SkColor color, SkColor color,
const gfx::Rect& bounds) { const gfx::Rect& bounds) {
const float scale = canvas->SaveAndUnscale(); gfx::ScopedCanvas scoped_canvas(canvas);
const float scale = canvas->UndoDeviceScaleFactor();
gfx::RectF rect(gfx::ScaleRect(gfx::RectF(bounds), scale)); gfx::RectF rect(gfx::ScaleRect(gfx::RectF(bounds), scale));
rect.Inset(0, rect.height() - 1, 0, 0); rect.Inset(0, rect.height() - 1, 0, 0);
SkPaint paint; SkPaint paint;
paint.setColor(color); paint.setColor(color);
canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), paint); canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), paint);
canvas->Restore();
} }
void BrowserView::InitStatusBubble() { void BrowserView::InitStatusBubble() {
......
...@@ -1329,7 +1329,8 @@ void LocationBarView::PaintChildren(const ui::PaintContext& context) { ...@@ -1329,7 +1329,8 @@ void LocationBarView::PaintChildren(const ui::PaintContext& context) {
if (ui::MaterialDesignController::IsModeMaterial()) { if (ui::MaterialDesignController::IsModeMaterial()) {
gfx::Canvas* canvas = recorder.canvas(); gfx::Canvas* canvas = recorder.canvas();
const float scale = canvas->SaveAndUnscale(); gfx::ScopedCanvas scoped_canvas(canvas);
const float scale = canvas->UndoDeviceScaleFactor();
SkPaint paint; SkPaint paint;
paint.setStyle(SkPaint::Style::kStroke_Style); paint.setStyle(SkPaint::Style::kStroke_Style);
...@@ -1346,7 +1347,6 @@ void LocationBarView::PaintChildren(const ui::PaintContext& context) { ...@@ -1346,7 +1347,6 @@ void LocationBarView::PaintChildren(const ui::PaintContext& context) {
const SkScalar kCornerRadius = SkDoubleToScalar(2.5f * scale); const SkScalar kCornerRadius = SkDoubleToScalar(2.5f * scale);
canvas->sk_canvas()->drawRoundRect(gfx::RectFToSkRect(border_rect_f), canvas->sk_canvas()->drawRoundRect(gfx::RectFToSkRect(border_rect_f),
kCornerRadius, kCornerRadius, paint); kCornerRadius, kCornerRadius, paint);
recorder.canvas()->Restore();
} else { } else {
views::Painter::PaintPainterAt(recorder.canvas(), border_painter_.get(), views::Painter::PaintPainterAt(recorder.canvas(), border_painter_.get(),
border_rect); border_rect);
......
...@@ -175,17 +175,16 @@ void Canvas::DrawDashedRect(const Rect& rect, SkColor color) { ...@@ -175,17 +175,16 @@ void Canvas::DrawDashedRect(const Rect& rect, SkColor color) {
paint); paint);
} }
void Canvas::Save() { float Canvas::UndoDeviceScaleFactor() {
canvas_->save();
}
float Canvas::SaveAndUnscale() {
Save();
SkScalar scale_factor = 1.0f / image_scale_; SkScalar scale_factor = 1.0f / image_scale_;
canvas_->scale(scale_factor, scale_factor); canvas_->scale(scale_factor, scale_factor);
return image_scale_; return image_scale_;
} }
void Canvas::Save() {
canvas_->save();
}
void Canvas::SaveLayerAlpha(uint8 alpha) { void Canvas::SaveLayerAlpha(uint8 alpha) {
canvas_->saveLayerAlpha(NULL, alpha); canvas_->saveLayerAlpha(NULL, alpha);
} }
......
...@@ -165,15 +165,15 @@ class GFX_EXPORT Canvas { ...@@ -165,15 +165,15 @@ class GFX_EXPORT Canvas {
// Draws a dashed rectangle of the specified color. // Draws a dashed rectangle of the specified color.
void DrawDashedRect(const Rect& rect, SkColor color); void DrawDashedRect(const Rect& rect, SkColor color);
// Unscales by the image scale factor (aka device scale factor), and returns
// that factor. This is useful when callers want to draw directly in the
// native scale.
float UndoDeviceScaleFactor();
// Saves a copy of the drawing state onto a stack, operating on this copy // Saves a copy of the drawing state onto a stack, operating on this copy
// until a balanced call to Restore() is made. // until a balanced call to Restore() is made.
void Save(); void Save();
// Saves the drawing state, unscales by the image scale factor, and returns
// that factor. This is useful when callers want to draw directly in the
// native scale.
float SaveAndUnscale();
// As with Save(), except draws to a layer that is blended with the canvas // As with Save(), except draws to a layer that is blended with the canvas
// at the specified alpha once Restore() is called. // at the specified alpha once Restore() is called.
// |layer_bounds| are the bounds of the layer relative to the current // |layer_bounds| are the bounds of the layer relative to the current
......
...@@ -88,8 +88,11 @@ void NineImagePainter::Paint(Canvas* canvas, ...@@ -88,8 +88,11 @@ void NineImagePainter::Paint(Canvas* canvas,
if (IsEmpty()) if (IsEmpty())
return; return;
// Painting at physical device pixels (undo device scale factor). ScopedCanvas scoped_canvas(canvas);
float scale = canvas->SaveAndUnscale();
// Painting and doing layout at physical device pixels to avoid cracks or
// overlap.
const float scale = canvas->UndoDeviceScaleFactor();
// Since the drawing from the following Fill() calls assumes the mapped origin // Since the drawing from the following Fill() calls assumes the mapped origin
// is at (0,0), we need to translate the canvas to the mapped origin. // is at (0,0), we need to translate the canvas to the mapped origin.
...@@ -163,8 +166,6 @@ void NineImagePainter::Paint(Canvas* canvas, ...@@ -163,8 +166,6 @@ void NineImagePainter::Paint(Canvas* canvas,
Fill(canvas, image_reps[8], width_in_pixels - i8w, height_in_pixels - i8h, Fill(canvas, image_reps[8], width_in_pixels - i8w, height_in_pixels - i8h,
i8w, i8h, paint); i8w, i8h, paint);
} }
canvas->Restore();
} }
// static // static
......
...@@ -20,12 +20,6 @@ class ScopedCanvas { ...@@ -20,12 +20,6 @@ class ScopedCanvas {
if (canvas_) if (canvas_)
canvas_->Restore(); canvas_->Restore();
} }
void SetCanvas(gfx::Canvas* canvas) {
if (canvas_)
canvas_->Restore();
canvas_ = canvas;
canvas_->Save();
}
private: private:
gfx::Canvas* canvas_; gfx::Canvas* canvas_;
......
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