| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/gfx/chrome_canvas.h" | 5 #include "app/gfx/canvas.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "app/gfx/chrome_font.h" | 9 #include "app/gfx/font.h" |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "base/gfx/rect.h" | 11 #include "base/gfx/rect.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "third_party/skia/include/core/SkShader.h" | 13 #include "third_party/skia/include/core/SkShader.h" |
| 14 | 14 |
| 15 bool ChromeCanvas::GetClipRect(gfx::Rect* r) { | 15 namespace gfx { |
| 16 |
| 17 bool Canvas::GetClipRect(gfx::Rect* r) { |
| 16 SkRect clip; | 18 SkRect clip; |
| 17 if (!getClipBounds(&clip)) { | 19 if (!getClipBounds(&clip)) { |
| 18 if (r) | 20 if (r) |
| 19 r->SetRect(0, 0, 0, 0); | 21 r->SetRect(0, 0, 0, 0); |
| 20 return false; | 22 return false; |
| 21 } | 23 } |
| 22 r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop), | 24 r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop), |
| 23 SkScalarRound(clip.fRight - clip.fLeft), | 25 SkScalarRound(clip.fRight - clip.fLeft), |
| 24 SkScalarRound(clip.fBottom - clip.fTop)); | 26 SkScalarRound(clip.fBottom - clip.fTop)); |
| 25 return true; | 27 return true; |
| 26 } | 28 } |
| 27 | 29 |
| 28 bool ChromeCanvas::ClipRectInt(int x, int y, int w, int h) { | 30 bool Canvas::ClipRectInt(int x, int y, int w, int h) { |
| 29 SkRect new_clip; | 31 SkRect new_clip; |
| 30 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), | 32 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), |
| 31 SkIntToScalar(x + w), SkIntToScalar(y + h)); | 33 SkIntToScalar(x + w), SkIntToScalar(y + h)); |
| 32 return clipRect(new_clip); | 34 return clipRect(new_clip); |
| 33 } | 35 } |
| 34 | 36 |
| 35 bool ChromeCanvas::IntersectsClipRectInt(int x, int y, int w, int h) { | 37 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { |
| 36 SkRect clip; | 38 SkRect clip; |
| 37 return getClipBounds(&clip) && | 39 return getClipBounds(&clip) && |
| 38 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | 40 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
| 39 SkIntToScalar(y + h)); | 41 SkIntToScalar(y + h)); |
| 40 } | 42 } |
| 41 | 43 |
| 42 void ChromeCanvas::TranslateInt(int x, int y) { | 44 void Canvas::TranslateInt(int x, int y) { |
| 43 translate(SkIntToScalar(x), SkIntToScalar(y)); | 45 translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 44 } | 46 } |
| 45 | 47 |
| 46 void ChromeCanvas::ScaleInt(int x, int y) { | 48 void Canvas::ScaleInt(int x, int y) { |
| 47 scale(SkIntToScalar(x), SkIntToScalar(y)); | 49 scale(SkIntToScalar(x), SkIntToScalar(y)); |
| 48 } | 50 } |
| 49 | 51 |
| 50 void ChromeCanvas::FillRectInt(const SkColor& color, | 52 void Canvas::FillRectInt(const SkColor& color, int x, int y, int w, int h) { |
| 51 int x, int y, int w, int h) { | |
| 52 SkPaint paint; | 53 SkPaint paint; |
| 53 paint.setColor(color); | 54 paint.setColor(color); |
| 54 paint.setStyle(SkPaint::kFill_Style); | 55 paint.setStyle(SkPaint::kFill_Style); |
| 55 paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode); | 56 paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode); |
| 56 FillRectInt(x, y, w, h, paint); | 57 FillRectInt(x, y, w, h, paint); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void ChromeCanvas::FillRectInt(int x, int y, int w, int h, | 60 void Canvas::FillRectInt(int x, int y, int w, int h, const SkPaint& paint) { |
| 60 const SkPaint& paint) { | |
| 61 SkIRect rc = {x, y, x + w, y + h}; | 61 SkIRect rc = {x, y, x + w, y + h}; |
| 62 drawIRect(rc, paint); | 62 drawIRect(rc, paint); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void ChromeCanvas::DrawRectInt(const SkColor& color, | 65 void Canvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h) { |
| 66 int x, int y, int w, int h) { | |
| 67 DrawRectInt(color, x, y, w, h, SkPorterDuff::kSrcOver_Mode); | 66 DrawRectInt(color, x, y, w, h, SkPorterDuff::kSrcOver_Mode); |
| 68 } | 67 } |
| 69 | 68 |
| 70 void ChromeCanvas::DrawRectInt(const SkColor& color, | 69 void Canvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h, |
| 71 int x, int y, int w, int h, | 70 SkPorterDuff::Mode mode) { |
| 72 SkPorterDuff::Mode mode) { | |
| 73 SkPaint paint; | 71 SkPaint paint; |
| 74 paint.setColor(color); | 72 paint.setColor(color); |
| 75 paint.setStyle(SkPaint::kStroke_Style); | 73 paint.setStyle(SkPaint::kStroke_Style); |
| 76 // Set a stroke width of 0, which will put us down the stroke rect path. If | 74 // Set a stroke width of 0, which will put us down the stroke rect path. If |
| 77 // we set a stroke width of 1, for example, this will internally create a | 75 // we set a stroke width of 1, for example, this will internally create a |
| 78 // path and fill it, which causes problems near the edge of the canvas. | 76 // path and fill it, which causes problems near the edge of the canvas. |
| 79 paint.setStrokeWidth(SkIntToScalar(0)); | 77 paint.setStrokeWidth(SkIntToScalar(0)); |
| 80 paint.setPorterDuffXfermode(mode); | 78 paint.setPorterDuffXfermode(mode); |
| 81 | 79 |
| 82 SkIRect rc = {x, y, x + w, y + h}; | 80 SkIRect rc = {x, y, x + w, y + h}; |
| 83 drawIRect(rc, paint); | 81 drawIRect(rc, paint); |
| 84 } | 82 } |
| 85 | 83 |
| 86 void ChromeCanvas::DrawLineInt(const SkColor& color, | 84 void Canvas::DrawLineInt(const SkColor& color, int x1, int y1, int x2, int y2) { |
| 87 int x1, int y1, int x2, int y2) { | |
| 88 SkPaint paint; | 85 SkPaint paint; |
| 89 paint.setColor(color); | 86 paint.setColor(color); |
| 90 paint.setStrokeWidth(SkIntToScalar(1)); | 87 paint.setStrokeWidth(SkIntToScalar(1)); |
| 91 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), | 88 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), |
| 92 SkIntToScalar(y2), paint); | 89 SkIntToScalar(y2), paint); |
| 93 } | 90 } |
| 94 | 91 |
| 95 void ChromeCanvas::DrawFocusRect(int x, int y, int width, int height) { | 92 void Canvas::DrawFocusRect(int x, int y, int width, int height) { |
| 96 // Create a 2D bitmap containing alternating on/off pixels - we do this | 93 // Create a 2D bitmap containing alternating on/off pixels - we do this |
| 97 // so that you never get two pixels of the same color around the edges | 94 // so that you never get two pixels of the same color around the edges |
| 98 // of the focus rect (this may mean that opposing edges of the rect may | 95 // of the focus rect (this may mean that opposing edges of the rect may |
| 99 // have a dot pattern out of phase to each other). | 96 // have a dot pattern out of phase to each other). |
| 100 static SkBitmap* dots = NULL; | 97 static SkBitmap* dots = NULL; |
| 101 if (!dots) { | 98 if (!dots) { |
| 102 int col_pixels = 32; | 99 int col_pixels = 32; |
| 103 int row_pixels = 32; | 100 int row_pixels = 32; |
| 104 | 101 |
| 105 dots = new SkBitmap; | 102 dots = new SkBitmap; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 drawRect(rect, paint); | 136 drawRect(rect, paint); |
| 140 | 137 |
| 141 rect.set(SkIntToScalar(x), SkIntToScalar(y), | 138 rect.set(SkIntToScalar(x), SkIntToScalar(y), |
| 142 SkIntToScalar(x + 1), SkIntToScalar(y + height)); | 139 SkIntToScalar(x + 1), SkIntToScalar(y + height)); |
| 143 drawRect(rect, paint); | 140 drawRect(rect, paint); |
| 144 rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y), | 141 rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y), |
| 145 SkIntToScalar(x + width), SkIntToScalar(y + height)); | 142 SkIntToScalar(x + width), SkIntToScalar(y + height)); |
| 146 drawRect(rect, paint); | 143 drawRect(rect, paint); |
| 147 } | 144 } |
| 148 | 145 |
| 149 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { | 146 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { |
| 150 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); | 147 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); |
| 151 } | 148 } |
| 152 | 149 |
| 153 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y, | 150 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y, |
| 154 const SkPaint& paint) { | 151 const SkPaint& paint) { |
| 155 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); | 152 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 156 } | 153 } |
| 157 | 154 |
| 158 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, | 155 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, |
| 159 int src_w, int src_h, int dest_x, int dest_y, | 156 int src_w, int src_h, int dest_x, int dest_y, |
| 160 int dest_w, int dest_h, | 157 int dest_w, int dest_h, |
| 161 bool filter) { | 158 bool filter) { |
| 162 SkPaint p; | 159 SkPaint p; |
| 163 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, | 160 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, |
| 164 dest_w, dest_h, filter, p); | 161 dest_w, dest_h, filter, p); |
| 165 } | 162 } |
| 166 | 163 |
| 167 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, | 164 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, |
| 168 int src_w, int src_h, int dest_x, int dest_y, | 165 int src_w, int src_h, int dest_x, int dest_y, |
| 169 int dest_w, int dest_h, | 166 int dest_w, int dest_h, |
| 170 bool filter, const SkPaint& paint) { | 167 bool filter, const SkPaint& paint) { |
| 171 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 168 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
| 172 src_y + src_h < std::numeric_limits<int16_t>::max()); | 169 src_y + src_h < std::numeric_limits<int16_t>::max()); |
| 173 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { | 170 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { |
| 174 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; | 171 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; |
| 175 return; | 172 return; |
| 176 } | 173 } |
| 177 | 174 |
| 178 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 175 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) |
| 179 return; | 176 return; |
| 180 | 177 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 209 // by the paint). | 206 // by the paint). |
| 210 SkPaint p(paint); | 207 SkPaint p(paint); |
| 211 p.setFilterBitmap(filter); | 208 p.setFilterBitmap(filter); |
| 212 p.setShader(shader); | 209 p.setShader(shader); |
| 213 shader->unref(); | 210 shader->unref(); |
| 214 | 211 |
| 215 // The rect will be filled by the bitmap. | 212 // The rect will be filled by the bitmap. |
| 216 drawRect(dest_rect, p); | 213 drawRect(dest_rect, p); |
| 217 } | 214 } |
| 218 | 215 |
| 219 void ChromeCanvas::DrawStringInt(const std::wstring& text, | 216 void Canvas::DrawStringInt(const std::wstring& text, |
| 220 const gfx::Font& font, | 217 const gfx::Font& font, |
| 221 const SkColor& color, | 218 const SkColor& color, |
| 222 int x, int y, | 219 int x, int y, int w, int h) { |
| 223 int w, int h) { | |
| 224 DrawStringInt(text, font, color, x, y, w, h, | 220 DrawStringInt(text, font, color, x, y, w, h, |
| 225 l10n_util::DefaultCanvasTextAlignment()); | 221 l10n_util::DefaultCanvasTextAlignment()); |
| 226 } | 222 } |
| 227 | 223 |
| 228 void ChromeCanvas::TileImageInt(const SkBitmap& bitmap, | 224 void Canvas::TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h) { |
| 229 int x, int y, int w, int h) { | |
| 230 TileImageInt(bitmap, 0, 0, x, y, w, h); | 225 TileImageInt(bitmap, 0, 0, x, y, w, h); |
| 231 } | 226 } |
| 232 | 227 |
| 233 void ChromeCanvas::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, | 228 void Canvas::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, |
| 234 int dest_x, int dest_y, int w, int h) { | 229 int dest_x, int dest_y, int w, int h) { |
| 235 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | 230 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) |
| 236 return; | 231 return; |
| 237 | 232 |
| 238 SkPaint paint; | 233 SkPaint paint; |
| 239 | 234 |
| 240 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | 235 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
| 241 SkShader::kRepeat_TileMode, | 236 SkShader::kRepeat_TileMode, |
| 242 SkShader::kRepeat_TileMode); | 237 SkShader::kRepeat_TileMode); |
| 243 paint.setShader(shader); | 238 paint.setShader(shader); |
| 244 paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode); | 239 paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode); |
| 245 | 240 |
| 246 // CreateBitmapShader returns a Shader with a reference count of one, we | 241 // CreateBitmapShader returns a Shader with a reference count of one, we |
| 247 // need to unref after paint takes ownership of the shader. | 242 // need to unref after paint takes ownership of the shader. |
| 248 shader->unref(); | 243 shader->unref(); |
| 249 save(); | 244 save(); |
| 250 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); | 245 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); |
| 251 ClipRectInt(src_x, src_y, w, h); | 246 ClipRectInt(src_x, src_y, w, h); |
| 252 drawPaint(paint); | 247 drawPaint(paint); |
| 253 restore(); | 248 restore(); |
| 254 } | 249 } |
| 255 | 250 |
| 256 SkBitmap ChromeCanvas::ExtractBitmap() { | 251 SkBitmap Canvas::ExtractBitmap() { |
| 257 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); | 252 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); |
| 258 | 253 |
| 259 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 254 // Make a bitmap to return, and a canvas to draw into it. We don't just want |
| 260 // to call extractSubset or the copy constuctor, since we want an actual copy | 255 // to call extractSubset or the copy constuctor, since we want an actual copy |
| 261 // of the bitmap. | 256 // of the bitmap. |
| 262 SkBitmap result; | 257 SkBitmap result; |
| 263 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 258 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
| 264 return result; | 259 return result; |
| 265 } | 260 } |
| 261 |
| 262 } // namespace gfx |
| OLD | NEW |