OLD | NEW |
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" |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 SkPaint p; | 271 SkPaint p; |
272 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, | 272 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, |
273 dest_w, dest_h, filter, p); | 273 dest_w, dest_h, filter, p); |
274 } | 274 } |
275 | 275 |
276 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, | 276 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, |
277 int src_x, int src_y, int src_w, int src_h, | 277 int src_x, int src_y, int src_w, int src_h, |
278 int dest_x, int dest_y, int dest_w, int dest_h, | 278 int dest_x, int dest_y, int dest_w, int dest_h, |
279 bool filter, | 279 bool filter, |
280 const SkPaint& paint) { | 280 const SkPaint& paint) { |
| 281 DrawBitmapFloat(bitmap, static_cast<float>(src_x), static_cast<float>(src_y), |
| 282 static_cast<float>(src_w), static_cast<float>(src_h), |
| 283 static_cast<float>(dest_x), static_cast<float>(dest_y), |
| 284 static_cast<float>(dest_w), static_cast<float>(dest_h), |
| 285 filter, paint); |
| 286 } |
| 287 |
| 288 void Canvas::DrawBitmapFloat(const SkBitmap& bitmap, |
| 289 float src_x, float src_y, float src_w, float src_h, |
| 290 float dest_x, float dest_y, float dest_w, float dest_h, |
| 291 bool filter, |
| 292 const SkPaint& paint) { |
281 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 293 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
282 src_y + src_h < std::numeric_limits<int16_t>::max()); | 294 src_y + src_h < std::numeric_limits<int16_t>::max()); |
283 if (src_w <= 0 || src_h <= 0) { | 295 if (src_w <= 0 || src_h <= 0) { |
284 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 296 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
285 return; | 297 return; |
286 } | 298 } |
287 | 299 |
288 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 300 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) |
289 return; | 301 return; |
290 | 302 |
291 SkRect dest_rect = { SkIntToScalar(dest_x), | 303 SkRect dest_rect = { SkFloatToScalar(dest_x), |
292 SkIntToScalar(dest_y), | 304 SkFloatToScalar(dest_y), |
293 SkIntToScalar(dest_x + dest_w), | 305 SkFloatToScalar(dest_x + dest_w), |
294 SkIntToScalar(dest_y + dest_h) }; | 306 SkFloatToScalar(dest_y + dest_h) }; |
295 | 307 |
296 if (src_w == dest_w && src_h == dest_h) { | 308 if (src_w == dest_w && src_h == dest_h) { |
297 // Workaround for apparent bug in Skia that causes image to occasionally | 309 // Workaround for apparent bug in Skia that causes image to occasionally |
298 // shift. | 310 // shift. |
299 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; | 311 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; |
300 canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); | 312 canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); |
301 return; | 313 return; |
302 } | 314 } |
303 | 315 |
304 // Make a bitmap shader that contains the bitmap we want to draw. This is | 316 // Make a bitmap shader that contains the bitmap we want to draw. This is |
305 // basically what SkCanvas.drawBitmap does internally, but it gives us | 317 // basically what SkCanvas.drawBitmap does internally, but it gives us |
306 // more control over quality and will use the mipmap in the source image if | 318 // more control over quality and will use the mipmap in the source image if |
307 // it has one, whereas drawBitmap won't. | 319 // it has one, whereas drawBitmap won't. |
308 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | 320 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
309 SkShader::kRepeat_TileMode, | 321 SkShader::kRepeat_TileMode, |
310 SkShader::kRepeat_TileMode); | 322 SkShader::kRepeat_TileMode); |
311 SkMatrix shader_scale; | 323 SkMatrix shader_scale; |
312 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), | 324 shader_scale.setScale(SkFloatToScalar(dest_w / src_w), |
313 SkFloatToScalar(static_cast<float>(dest_h) / src_h)); | 325 SkFloatToScalar(dest_h / src_h)); |
314 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 326 shader_scale.preTranslate(SkFloatToScalar(-src_x), SkFloatToScalar(-src_y)); |
315 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 327 shader_scale.postTranslate(SkFloatToScalar(dest_x), SkFloatToScalar(dest_y)); |
316 shader->setLocalMatrix(shader_scale); | 328 shader->setLocalMatrix(shader_scale); |
317 | 329 |
318 // Set up our paint to use the shader & release our reference (now just owned | 330 // Set up our paint to use the shader & release our reference (now just owned |
319 // by the paint). | 331 // by the paint). |
320 SkPaint p(paint); | 332 SkPaint p(paint); |
321 p.setFilterBitmap(filter); | 333 p.setFilterBitmap(filter); |
322 p.setShader(shader); | 334 p.setShader(shader); |
323 shader->unref(); | 335 shader->unref(); |
324 | 336 |
325 // The rect will be filled by the bitmap. | 337 // The rect will be filled by the bitmap. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | 414 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
403 SkIntToScalar(y + h)); | 415 SkIntToScalar(y + h)); |
404 } | 416 } |
405 | 417 |
406 bool Canvas::IntersectsClipRect(const gfx::Rect& rect) { | 418 bool Canvas::IntersectsClipRect(const gfx::Rect& rect) { |
407 return IntersectsClipRectInt(rect.x(), rect.y(), | 419 return IntersectsClipRectInt(rect.x(), rect.y(), |
408 rect.width(), rect.height()); | 420 rect.width(), rect.height()); |
409 } | 421 } |
410 | 422 |
411 } // namespace gfx | 423 } // namespace gfx |
OLD | NEW |