summaryrefslogtreecommitdiffstats
path: root/ui/gfx/canvas.cc
diff options
context:
space:
mode:
authorpkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-02 19:41:15 +0000
committerpkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-02 19:41:15 +0000
commit8232da6b7154568c250bbce73052402f75475b24 (patch)
tree9f0a6a9c2066ec71a5e63a76d26a6edaf471586e /ui/gfx/canvas.cc
parent0950eaef0e83ee0ea36f7cf88bb248464b6d45d0 (diff)
downloadchromium_src-8232da6b7154568c250bbce73052402f75475b24.zip
chromium_src-8232da6b7154568c250bbce73052402f75475b24.tar.gz
chromium_src-8232da6b7154568c250bbce73052402f75475b24.tar.bz2
Adds factory method to create Bitmap shader which displays correctly in High DPI
Added DrawImageInPath to Canvas API. BUG=None TEST=Manual Test Steps: Change the resize code in tray_user to properly resize based on scale factor (See TODO) Run with --force-device-scale-factor=2 --load-2x-resources Ensure that tray user avatars are not blurry Review URL: https://chromiumcodereview.appspot.com/10720003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@145176 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/canvas.cc')
-rw-r--r--ui/gfx/canvas.cc108
1 files changed, 58 insertions, 50 deletions
diff --git a/ui/gfx/canvas.cc b/ui/gfx/canvas.cc
index f0208a3..26f2e30 100644
--- a/ui/gfx/canvas.cc
+++ b/ui/gfx/canvas.cc
@@ -307,10 +307,11 @@ void Canvas::DrawImageInt(const gfx::ImageSkia& image, int x, int y) {
void Canvas::DrawImageInt(const gfx::ImageSkia& image,
int x, int y,
const SkPaint& paint) {
- float bitmap_scale;
- const SkBitmap& bitmap = GetBitmapToPaint(image, &bitmap_scale);
- if (bitmap.isNull())
+ const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image);
+ if (image_rep.is_null())
return;
+ const SkBitmap& bitmap = image_rep.sk_bitmap();
+ float bitmap_scale = image_rep.GetScale();
canvas_->save();
canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale),
@@ -349,10 +350,9 @@ void Canvas::DrawImageInt(const gfx::ImageSkia& image,
float user_scale_x = static_cast<float>(dest_w) / src_w;
float user_scale_y = static_cast<float>(dest_h) / src_h;
- float bitmap_scale;
- const SkBitmap& bitmap = GetBitmapToPaint(image, user_scale_x, user_scale_y,
- &bitmap_scale);
- if (bitmap.isNull())
+ const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image,
+ user_scale_x, user_scale_y);
+ if (image_rep.is_null())
return;
SkRect dest_rect = { SkIntToScalar(dest_x),
@@ -361,10 +361,12 @@ void Canvas::DrawImageInt(const gfx::ImageSkia& image,
SkIntToScalar(dest_y + dest_h) };
if (src_w == dest_w && src_h == dest_h &&
- user_scale_x == 1.0f && user_scale_y == 1.0f && bitmap_scale == 1.0f) {
+ user_scale_x == 1.0f && user_scale_y == 1.0f &&
+ image_rep.scale_factor() == ui::SCALE_FACTOR_100P) {
// Workaround for apparent bug in Skia that causes image to occasionally
// shift.
SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
+ const SkBitmap& bitmap = image_rep.sk_bitmap();
canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
return;
}
@@ -373,19 +375,15 @@ void Canvas::DrawImageInt(const gfx::ImageSkia& image,
// basically what SkCanvas.drawBitmap does internally, but it gives us
// more control over quality and will use the mipmap in the source image if
// it has one, whereas drawBitmap won't.
- SkShader* shader = SkShader::CreateBitmapShader(bitmap,
- SkShader::kRepeat_TileMode,
- SkShader::kRepeat_TileMode);
SkMatrix shader_scale;
shader_scale.setScale(SkFloatToScalar(user_scale_x),
SkFloatToScalar(user_scale_y));
- shader_scale.preTranslate(SkFloatToScalar(-src_x * bitmap_scale),
- SkFloatToScalar(-src_y * bitmap_scale));
- shader_scale.postTranslate(SkFloatToScalar(dest_x * bitmap_scale),
- SkFloatToScalar(dest_y * bitmap_scale));
- shader_scale.postScale(SkFloatToScalar(1.0f / bitmap_scale),
- SkFloatToScalar(1.0f / bitmap_scale));
- shader->setLocalMatrix(shader_scale);
+ shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
+ shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
+
+ SkShader* shader = gfx::CreateImageRepShader(image_rep,
+ SkShader::kRepeat_TileMode,
+ shader_scale);
// Set up our paint to use the shader & release our reference (now just owned
// by the paint).
@@ -398,6 +396,26 @@ void Canvas::DrawImageInt(const gfx::ImageSkia& image,
canvas_->drawRect(dest_rect, p);
}
+void Canvas::DrawImageInPath(const gfx::ImageSkia& image,
+ int x,
+ int y,
+ const SkPath& path,
+ const SkPaint& paint) {
+ const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image);
+ if (image_rep.is_null())
+ return;
+
+ SkMatrix matrix;
+ matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
+ SkShader* shader = gfx::CreateImageRepShader(image_rep,
+ SkShader::kRepeat_TileMode, matrix);
+
+ SkPaint p(paint);
+ p.setShader(shader);
+ shader->unref();
+ canvas_->drawPath(path, p);
+}
+
void Canvas::DrawStringInt(const string16& text,
const gfx::Font& font,
SkColor color,
@@ -444,31 +462,22 @@ void Canvas::TileImageInt(const gfx::ImageSkia& image,
if (!IntersectsClipRectInt(dest_x, dest_y, w, h))
return;
- float bitmap_scale;
- const SkBitmap& bitmap = GetBitmapToPaint(image,
- tile_scale_x,
- tile_scale_y,
- &bitmap_scale);
- if (bitmap.isNull())
+ const gfx::ImageSkiaRep& image_rep = GetImageRepToPaint(image,
+ tile_scale_x, tile_scale_y);
+ if (image_rep.is_null())
return;
- SkPaint paint;
-
- SkShader* shader = SkShader::CreateBitmapShader(bitmap,
- SkShader::kRepeat_TileMode,
- SkShader::kRepeat_TileMode);
-
SkMatrix shader_scale;
shader_scale.setScale(SkFloatToScalar(tile_scale_x),
SkFloatToScalar(tile_scale_y));
- shader_scale.preTranslate(SkFloatToScalar(-src_x * bitmap_scale),
- SkFloatToScalar(-src_y * bitmap_scale));
- shader_scale.postTranslate(SkFloatToScalar(dest_x * bitmap_scale),
- SkFloatToScalar(dest_y * bitmap_scale));
- shader_scale.postScale(SkFloatToScalar(1.0f / bitmap_scale),
- SkFloatToScalar(1.0f / bitmap_scale));
- shader->setLocalMatrix(shader_scale);
+ shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
+ shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
+
+ SkShader* shader = gfx::CreateImageRepShader(image_rep,
+ SkShader::kRepeat_TileMode,
+ shader_scale);
+ SkPaint paint;
paint.setShader(shader);
paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
shader->unref();
@@ -514,31 +523,30 @@ void Canvas::ApplyScaleFactor(ui::ScaleFactor scale_factor,
}
}
-const SkBitmap& Canvas::GetBitmapToPaint(const gfx::ImageSkia& image,
- float* bitmap_scale_factor) const {
- return GetBitmapToPaint(image, 1.0f, 1.0f, bitmap_scale_factor);
+const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint(
+ const gfx::ImageSkia& image) const {
+ return GetImageRepToPaint(image, 1.0f, 1.0f);
}
-const SkBitmap& Canvas::GetBitmapToPaint(const gfx::ImageSkia& image,
- float user_additional_scale_x,
- float user_additional_scale_y,
- float* bitmap_scale_factor) const {
+const gfx::ImageSkiaRep& Canvas::GetImageRepToPaint(
+ const gfx::ImageSkia& image,
+ float user_additional_scale_x,
+ float user_additional_scale_y) const {
const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor_);
- const SkBitmap& bitmap = image_rep.sk_bitmap();
- *bitmap_scale_factor = image_rep.GetScale();
- if (!bitmap.isNull()) {
+ if (!image_rep.is_null()) {
SkMatrix m = canvas_->getTotalMatrix();
float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) *
user_additional_scale_x;
float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) *
user_additional_scale_y;
- if (scale_x < *bitmap_scale_factor || scale_y < *bitmap_scale_factor)
- const_cast<SkBitmap&>(bitmap).buildMipMap();
+ float bitmap_scale = image_rep.GetScale();
+ if (scale_x < bitmap_scale || scale_y < bitmap_scale)
+ const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap();
}
- return bitmap;
+ return image_rep;
}
} // namespace gfx