OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_skia.h" | 5 #include "ui/gfx/canvas_skia.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 29 matching lines...) Expand all Loading... |
40 }; | 40 }; |
41 | 41 |
42 | 42 |
43 } // namespace | 43 } // namespace |
44 | 44 |
45 namespace gfx { | 45 namespace gfx { |
46 | 46 |
47 //////////////////////////////////////////////////////////////////////////////// | 47 //////////////////////////////////////////////////////////////////////////////// |
48 // CanvasSkia, public: | 48 // CanvasSkia, public: |
49 | 49 |
| 50 CanvasSkia::CanvasSkia() { |
| 51 } |
| 52 |
| 53 CanvasSkia::~CanvasSkia() { |
| 54 } |
| 55 |
50 // static | 56 // static |
51 int CanvasSkia::DefaultCanvasTextAlignment() { | 57 int CanvasSkia::DefaultCanvasTextAlignment() { |
52 if (!base::i18n::IsRTL()) | 58 if (!base::i18n::IsRTL()) |
53 return gfx::Canvas::TEXT_ALIGN_LEFT; | 59 return gfx::Canvas::TEXT_ALIGN_LEFT; |
54 return gfx::Canvas::TEXT_ALIGN_RIGHT; | 60 return gfx::Canvas::TEXT_ALIGN_RIGHT; |
55 } | 61 } |
56 | 62 |
| 63 bool CanvasSkia::Init(int width, int height, bool is_opaque) { |
| 64 skia_canvas_.reset(skia::CreateBitmapCanvas(width, height, is_opaque)); |
| 65 return skia_canvas_.get() ? true : false; |
| 66 } |
| 67 |
57 SkBitmap CanvasSkia::ExtractBitmap() const { | 68 SkBitmap CanvasSkia::ExtractBitmap() const { |
58 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); | 69 const SkBitmap& device_bitmap = |
| 70 skia_canvas_->getDevice()->accessBitmap(false); |
59 | 71 |
60 // Make a bitmap to return, and a canvas to draw into it. We don't just want | 72 // Make a bitmap to return, and a canvas to draw into it. We don't just want |
61 // to call extractSubset or the copy constructor, since we want an actual copy | 73 // to call extractSubset or the copy constructor, since we want an actual copy |
62 // of the bitmap. | 74 // of the bitmap. |
63 SkBitmap result; | 75 SkBitmap result; |
64 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | 76 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); |
65 return result; | 77 return result; |
66 } | 78 } |
67 | 79 |
68 //////////////////////////////////////////////////////////////////////////////// | 80 //////////////////////////////////////////////////////////////////////////////// |
69 // CanvasSkia, Canvas implementation: | 81 // CanvasSkia, Canvas implementation: |
70 | 82 |
71 void CanvasSkia::Save() { | 83 void CanvasSkia::Save() { |
72 save(); | 84 skia_canvas_->save(); |
73 } | 85 } |
74 | 86 |
75 void CanvasSkia::SaveLayerAlpha(uint8 alpha) { | 87 void CanvasSkia::SaveLayerAlpha(uint8 alpha) { |
76 saveLayerAlpha(NULL, alpha); | 88 skia_canvas_->saveLayerAlpha(NULL, alpha); |
77 } | 89 } |
78 | 90 |
79 | 91 |
80 void CanvasSkia::SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) { | 92 void CanvasSkia::SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) { |
81 SkRect bounds; | 93 SkRect bounds; |
82 bounds.set(SkIntToScalar(layer_bounds.x()), | 94 bounds.set(SkIntToScalar(layer_bounds.x()), |
83 SkIntToScalar(layer_bounds.y()), | 95 SkIntToScalar(layer_bounds.y()), |
84 SkIntToScalar(layer_bounds.right()), | 96 SkIntToScalar(layer_bounds.right()), |
85 SkIntToScalar(layer_bounds.bottom())); | 97 SkIntToScalar(layer_bounds.bottom())); |
86 saveLayerAlpha(&bounds, alpha); | 98 skia_canvas_->saveLayerAlpha(&bounds, alpha); |
87 } | 99 } |
88 | 100 |
89 void CanvasSkia::Restore() { | 101 void CanvasSkia::Restore() { |
90 restore(); | 102 skia_canvas_->restore(); |
91 } | 103 } |
92 | 104 |
93 bool CanvasSkia::ClipRectInt(int x, int y, int w, int h) { | 105 bool CanvasSkia::ClipRectInt(int x, int y, int w, int h) { |
94 SkRect new_clip; | 106 SkRect new_clip; |
95 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), | 107 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), |
96 SkIntToScalar(x + w), SkIntToScalar(y + h)); | 108 SkIntToScalar(x + w), SkIntToScalar(y + h)); |
97 return clipRect(new_clip); | 109 return skia_canvas_->clipRect(new_clip); |
98 } | 110 } |
99 | 111 |
100 void CanvasSkia::TranslateInt(int x, int y) { | 112 void CanvasSkia::TranslateInt(int x, int y) { |
101 translate(SkIntToScalar(x), SkIntToScalar(y)); | 113 skia_canvas_->translate(SkIntToScalar(x), SkIntToScalar(y)); |
102 } | 114 } |
103 | 115 |
104 void CanvasSkia::ScaleInt(int x, int y) { | 116 void CanvasSkia::ScaleInt(int x, int y) { |
105 scale(SkIntToScalar(x), SkIntToScalar(y)); | 117 skia_canvas_->scale(SkIntToScalar(x), SkIntToScalar(y)); |
106 } | 118 } |
107 | 119 |
108 void CanvasSkia::FillRectInt(const SkColor& color, int x, int y, int w, int h) { | 120 void CanvasSkia::FillRectInt(const SkColor& color, int x, int y, int w, int h) { |
109 FillRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode); | 121 FillRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode); |
110 } | 122 } |
111 | 123 |
112 void CanvasSkia::FillRectInt(const SkColor& color, | 124 void CanvasSkia::FillRectInt(const SkColor& color, |
113 int x, int y, int w, int h, | 125 int x, int y, int w, int h, |
114 SkXfermode::Mode mode) { | 126 SkXfermode::Mode mode) { |
115 SkPaint paint; | 127 SkPaint paint; |
(...skipping 26 matching lines...) Expand all Loading... |
142 // we set a stroke width of 1, for example, this will internally create a | 154 // we set a stroke width of 1, for example, this will internally create a |
143 // path and fill it, which causes problems near the edge of the canvas. | 155 // path and fill it, which causes problems near the edge of the canvas. |
144 paint.setStrokeWidth(SkIntToScalar(0)); | 156 paint.setStrokeWidth(SkIntToScalar(0)); |
145 paint.setXfermodeMode(mode); | 157 paint.setXfermodeMode(mode); |
146 | 158 |
147 DrawRectInt(x, y, w, h, paint); | 159 DrawRectInt(x, y, w, h, paint); |
148 } | 160 } |
149 | 161 |
150 void CanvasSkia::DrawRectInt(int x, int y, int w, int h, const SkPaint& paint) { | 162 void CanvasSkia::DrawRectInt(int x, int y, int w, int h, const SkPaint& paint) { |
151 SkIRect rc = { x, y, x + w, y + h }; | 163 SkIRect rc = { x, y, x + w, y + h }; |
152 drawIRect(rc, paint); | 164 skia_canvas_->drawIRect(rc, paint); |
153 } | 165 } |
154 | 166 |
155 void CanvasSkia::DrawLineInt(const SkColor& color, | 167 void CanvasSkia::DrawLineInt(const SkColor& color, |
156 int x1, int y1, | 168 int x1, int y1, |
157 int x2, int y2) { | 169 int x2, int y2) { |
158 SkPaint paint; | 170 SkPaint paint; |
159 paint.setColor(color); | 171 paint.setColor(color); |
160 paint.setStrokeWidth(SkIntToScalar(1)); | 172 paint.setStrokeWidth(SkIntToScalar(1)); |
161 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), | 173 skia_canvas_->drawLine(SkIntToScalar(x1), SkIntToScalar(y1), |
162 SkIntToScalar(y2), paint); | 174 SkIntToScalar(x2), SkIntToScalar(y2), |
| 175 paint); |
163 } | 176 } |
164 | 177 |
165 void CanvasSkia::DrawFocusRect(int x, int y, int width, int height) { | 178 void CanvasSkia::DrawFocusRect(int x, int y, int width, int height) { |
166 // Create a 2D bitmap containing alternating on/off pixels - we do this | 179 // Create a 2D bitmap containing alternating on/off pixels - we do this |
167 // so that you never get two pixels of the same color around the edges | 180 // so that you never get two pixels of the same color around the edges |
168 // of the focus rect (this may mean that opposing edges of the rect may | 181 // of the focus rect (this may mean that opposing edges of the rect may |
169 // have a dot pattern out of phase to each other). | 182 // have a dot pattern out of phase to each other). |
170 static SkBitmap* dots = NULL; | 183 static SkBitmap* dots = NULL; |
171 if (!dots) { | 184 if (!dots) { |
172 int col_pixels = 32; | 185 int col_pixels = 32; |
(...skipping 27 matching lines...) Expand all Loading... |
200 paint.setShader(shader); | 213 paint.setShader(shader); |
201 shader->unref(); | 214 shader->unref(); |
202 | 215 |
203 DrawRectInt(x, y, width, 1, paint); | 216 DrawRectInt(x, y, width, 1, paint); |
204 DrawRectInt(x, y + height - 1, width, 1, paint); | 217 DrawRectInt(x, y + height - 1, width, 1, paint); |
205 DrawRectInt(x, y, 1, height, paint); | 218 DrawRectInt(x, y, 1, height, paint); |
206 DrawRectInt(x + width - 1, y, 1, height, paint); | 219 DrawRectInt(x + width - 1, y, 1, height, paint); |
207 } | 220 } |
208 | 221 |
209 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { | 222 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { |
210 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); | 223 skia_canvas_->drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); |
211 } | 224 } |
212 | 225 |
213 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, | 226 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, |
214 int x, int y, | 227 int x, int y, |
215 const SkPaint& paint) { | 228 const SkPaint& paint) { |
216 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); | 229 skia_canvas_->drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
217 } | 230 } |
218 | 231 |
219 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, | 232 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, |
220 int src_x, int src_y, int src_w, int src_h, | 233 int src_x, int src_y, int src_w, int src_h, |
221 int dest_x, int dest_y, int dest_w, int dest_h, | 234 int dest_x, int dest_y, int dest_w, int dest_h, |
222 bool filter) { | 235 bool filter) { |
223 SkPaint p; | 236 SkPaint p; |
224 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, | 237 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, |
225 dest_w, dest_h, filter, p); | 238 dest_w, dest_h, filter, p); |
226 } | 239 } |
(...skipping 15 matching lines...) Expand all Loading... |
242 | 255 |
243 SkRect dest_rect = { SkIntToScalar(dest_x), | 256 SkRect dest_rect = { SkIntToScalar(dest_x), |
244 SkIntToScalar(dest_y), | 257 SkIntToScalar(dest_y), |
245 SkIntToScalar(dest_x + dest_w), | 258 SkIntToScalar(dest_x + dest_w), |
246 SkIntToScalar(dest_y + dest_h) }; | 259 SkIntToScalar(dest_y + dest_h) }; |
247 | 260 |
248 if (src_w == dest_w && src_h == dest_h) { | 261 if (src_w == dest_w && src_h == dest_h) { |
249 // Workaround for apparent bug in Skia that causes image to occasionally | 262 // Workaround for apparent bug in Skia that causes image to occasionally |
250 // shift. | 263 // shift. |
251 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; | 264 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; |
252 drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); | 265 skia_canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); |
253 return; | 266 return; |
254 } | 267 } |
255 | 268 |
256 // Make a bitmap shader that contains the bitmap we want to draw. This is | 269 // Make a bitmap shader that contains the bitmap we want to draw. This is |
257 // basically what SkCanvas.drawBitmap does internally, but it gives us | 270 // basically what SkCanvas.drawBitmap does internally, but it gives us |
258 // more control over quality and will use the mipmap in the source image if | 271 // more control over quality and will use the mipmap in the source image if |
259 // it has one, whereas drawBitmap won't. | 272 // it has one, whereas drawBitmap won't. |
260 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | 273 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
261 SkShader::kRepeat_TileMode, | 274 SkShader::kRepeat_TileMode, |
262 SkShader::kRepeat_TileMode); | 275 SkShader::kRepeat_TileMode); |
263 SkMatrix shader_scale; | 276 SkMatrix shader_scale; |
264 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), | 277 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), |
265 SkFloatToScalar(static_cast<float>(dest_h) / src_h)); | 278 SkFloatToScalar(static_cast<float>(dest_h) / src_h)); |
266 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 279 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
267 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 280 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
268 shader->setLocalMatrix(shader_scale); | 281 shader->setLocalMatrix(shader_scale); |
269 | 282 |
270 // Set up our paint to use the shader & release our reference (now just owned | 283 // Set up our paint to use the shader & release our reference (now just owned |
271 // by the paint). | 284 // by the paint). |
272 SkPaint p(paint); | 285 SkPaint p(paint); |
273 p.setFilterBitmap(filter); | 286 p.setFilterBitmap(filter); |
274 p.setShader(shader); | 287 p.setShader(shader); |
275 shader->unref(); | 288 shader->unref(); |
276 | 289 |
277 // The rect will be filled by the bitmap. | 290 // The rect will be filled by the bitmap. |
278 drawRect(dest_rect, p); | 291 skia_canvas_->drawRect(dest_rect, p); |
279 } | 292 } |
280 | 293 |
281 void CanvasSkia::DrawStringInt(const string16& text, | 294 void CanvasSkia::DrawStringInt(const string16& text, |
282 const gfx::Font& font, | 295 const gfx::Font& font, |
283 const SkColor& color, | 296 const SkColor& color, |
284 int x, int y, int w, int h) { | 297 int x, int y, int w, int h) { |
285 DrawStringInt(text, font, color, x, y, w, h, | 298 DrawStringInt(text, font, color, x, y, w, h, |
286 gfx::CanvasSkia::DefaultCanvasTextAlignment()); | 299 gfx::CanvasSkia::DefaultCanvasTextAlignment()); |
287 } | 300 } |
288 | 301 |
(...skipping 20 matching lines...) Expand all Loading... |
309 | 322 |
310 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | 323 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
311 SkShader::kRepeat_TileMode, | 324 SkShader::kRepeat_TileMode, |
312 SkShader::kRepeat_TileMode); | 325 SkShader::kRepeat_TileMode); |
313 paint.setShader(shader); | 326 paint.setShader(shader); |
314 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | 327 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
315 | 328 |
316 // CreateBitmapShader returns a Shader with a reference count of one, we | 329 // CreateBitmapShader returns a Shader with a reference count of one, we |
317 // need to unref after paint takes ownership of the shader. | 330 // need to unref after paint takes ownership of the shader. |
318 shader->unref(); | 331 shader->unref(); |
319 save(); | 332 skia_canvas_->save(); |
320 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); | 333 skia_canvas_->translate(SkIntToScalar(dest_x - src_x), |
| 334 SkIntToScalar(dest_y - src_y)); |
321 ClipRectInt(src_x, src_y, w, h); | 335 ClipRectInt(src_x, src_y, w, h); |
322 drawPaint(paint); | 336 skia_canvas_->drawPaint(paint); |
323 restore(); | 337 skia_canvas_->restore(); |
324 } | 338 } |
325 | 339 |
326 gfx::NativeDrawingContext CanvasSkia::BeginPlatformPaint() { | 340 gfx::NativeDrawingContext CanvasSkia::BeginPlatformPaint() { |
327 return beginPlatformPaint(); | 341 return skia::BeginPlatformPaint(skia_canvas_.get()); |
328 } | 342 } |
329 | 343 |
330 void CanvasSkia::EndPlatformPaint() { | 344 void CanvasSkia::EndPlatformPaint() { |
331 endPlatformPaint(); | 345 skia::EndPlatformPaint(skia_canvas_.get()); |
332 } | 346 } |
333 | 347 |
334 void CanvasSkia::Transform(const ui::Transform& transform) { | 348 void CanvasSkia::Transform(const ui::Transform& transform) { |
335 concat(*reinterpret_cast<const ui::TransformSkia&>(transform).matrix_.get()); | 349 skia_canvas_->concat( |
| 350 *reinterpret_cast<const ui::TransformSkia&>(transform).matrix_.get()); |
336 } | 351 } |
337 | 352 |
338 CanvasSkia* CanvasSkia::AsCanvasSkia() { | 353 CanvasSkia* CanvasSkia::AsCanvasSkia() { |
339 return this; | 354 return this; |
340 } | 355 } |
341 | 356 |
342 const CanvasSkia* CanvasSkia::AsCanvasSkia() const { | 357 const CanvasSkia* CanvasSkia::AsCanvasSkia() const { |
343 return this; | 358 return this; |
344 } | 359 } |
345 | 360 |
346 //////////////////////////////////////////////////////////////////////////////// | 361 //////////////////////////////////////////////////////////////////////////////// |
347 // CanvasSkia, private: | 362 // CanvasSkia, private: |
348 | 363 |
349 bool CanvasSkia::IntersectsClipRectInt(int x, int y, int w, int h) { | 364 bool CanvasSkia::IntersectsClipRectInt(int x, int y, int w, int h) { |
350 SkRect clip; | 365 SkRect clip; |
351 return getClipBounds(&clip) && | 366 return skia_canvas_->getClipBounds(&clip) && |
352 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | 367 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
353 SkIntToScalar(y + h)); | 368 SkIntToScalar(y + h)); |
354 } | 369 } |
355 | 370 |
356 //////////////////////////////////////////////////////////////////////////////// | 371 //////////////////////////////////////////////////////////////////////////////// |
357 // Canvas, public: | 372 // Canvas, public: |
358 | 373 |
359 Canvas* Canvas::CreateCanvas() { | 374 Canvas* Canvas::CreateCanvas() { |
360 return new CanvasSkia; | 375 return new CanvasSkia; |
361 } | 376 } |
362 | 377 |
363 Canvas* Canvas::CreateCanvas(int width, int height, bool is_opaque) { | 378 Canvas* Canvas::CreateCanvas(int width, int height, bool is_opaque) { |
364 return new CanvasSkia(width, height, is_opaque); | 379 CanvasSkia* canvas = new CanvasSkia; |
| 380 if (!canvas->Init(width, height, is_opaque)) { |
| 381 delete canvas; |
| 382 return NULL; |
| 383 } |
| 384 return canvas; |
365 } | 385 } |
366 | 386 |
367 #if defined(OS_WIN) | |
368 // TODO(beng): move to canvas_win.cc, etc. | |
369 class CanvasPaintWin : public CanvasSkiaPaint, public CanvasPaint { | |
370 public: | |
371 CanvasPaintWin(gfx::NativeView view) : CanvasSkiaPaint(view) {} | |
372 | |
373 // Overridden from CanvasPaint2: | |
374 virtual bool IsValid() const { | |
375 return isEmpty(); | |
376 } | |
377 | |
378 virtual gfx::Rect GetInvalidRect() const { | |
379 return gfx::Rect(paintStruct().rcPaint); | |
380 } | |
381 | |
382 virtual Canvas* AsCanvas() { | |
383 return this; | |
384 } | |
385 }; | |
386 #endif | |
387 | |
388 CanvasPaint* CanvasPaint::CreateCanvasPaint(gfx::NativeView view) { | 387 CanvasPaint* CanvasPaint::CreateCanvasPaint(gfx::NativeView view) { |
389 #if defined(OS_WIN) | 388 #if defined(OS_WIN) |
390 return new CanvasPaintWin(view); | 389 return new CanvasSkiaPaint(view); |
391 #else | 390 #else |
392 return NULL; | 391 return NULL; |
393 #endif | 392 #endif |
394 } | 393 } |
395 | 394 |
396 } // namespace gfx | 395 } // namespace gfx |
OLD | NEW |