Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: ui/gfx/canvas_skia.cc

Issue 8122013: Allow CanvasSkia to bind to an existing SkCanvas. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: 1 more fix Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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(int width, int height, bool is_opaque)
51 : owned_canvas_(new skia::PlatformCanvas(width, height, is_opaque)),
52 canvas_(owned_canvas_.get()) {
53 }
54
55 CanvasSkia::CanvasSkia()
56 : owned_canvas_(new skia::PlatformCanvas()),
57 canvas_(owned_canvas_.get()) {
58 }
59
60 CanvasSkia::CanvasSkia(SkCanvas* canvas)
61 : owned_canvas_(),
62 canvas_(canvas) {
63 DCHECK(canvas);
64 }
65
66 CanvasSkia::~CanvasSkia() {
67 }
68
50 // static 69 // static
51 int CanvasSkia::DefaultCanvasTextAlignment() { 70 int CanvasSkia::DefaultCanvasTextAlignment() {
52 if (!base::i18n::IsRTL()) 71 if (!base::i18n::IsRTL())
53 return gfx::Canvas::TEXT_ALIGN_LEFT; 72 return gfx::Canvas::TEXT_ALIGN_LEFT;
54 return gfx::Canvas::TEXT_ALIGN_RIGHT; 73 return gfx::Canvas::TEXT_ALIGN_RIGHT;
55 } 74 }
56 75
57 SkBitmap CanvasSkia::ExtractBitmap() const { 76 SkBitmap CanvasSkia::ExtractBitmap() const {
58 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); 77 const SkBitmap& device_bitmap = canvas_->getDevice()->accessBitmap(false);
59 78
60 // Make a bitmap to return, and a canvas to draw into it. We don't just want 79 // 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 80 // to call extractSubset or the copy constructor, since we want an actual copy
62 // of the bitmap. 81 // of the bitmap.
63 SkBitmap result; 82 SkBitmap result;
64 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); 83 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
65 return result; 84 return result;
66 } 85 }
67 86
68 //////////////////////////////////////////////////////////////////////////////// 87 ////////////////////////////////////////////////////////////////////////////////
69 // CanvasSkia, Canvas implementation: 88 // CanvasSkia, Canvas implementation:
70 89
71 void CanvasSkia::Save() { 90 void CanvasSkia::Save() {
72 save(); 91 canvas_->save();
73 } 92 }
74 93
75 void CanvasSkia::SaveLayerAlpha(uint8 alpha) { 94 void CanvasSkia::SaveLayerAlpha(uint8 alpha) {
76 saveLayerAlpha(NULL, alpha); 95 canvas_->saveLayerAlpha(NULL, alpha);
77 } 96 }
78 97
79 98
80 void CanvasSkia::SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) { 99 void CanvasSkia::SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) {
81 SkRect bounds; 100 SkRect bounds;
82 bounds.set(SkIntToScalar(layer_bounds.x()), 101 bounds.set(SkIntToScalar(layer_bounds.x()),
83 SkIntToScalar(layer_bounds.y()), 102 SkIntToScalar(layer_bounds.y()),
84 SkIntToScalar(layer_bounds.right()), 103 SkIntToScalar(layer_bounds.right()),
85 SkIntToScalar(layer_bounds.bottom())); 104 SkIntToScalar(layer_bounds.bottom()));
86 saveLayerAlpha(&bounds, alpha); 105 canvas_->saveLayerAlpha(&bounds, alpha);
87 } 106 }
88 107
89 void CanvasSkia::Restore() { 108 void CanvasSkia::Restore() {
90 restore(); 109 canvas_->restore();
91 } 110 }
92 111
93 bool CanvasSkia::ClipRectInt(int x, int y, int w, int h) { 112 bool CanvasSkia::ClipRectInt(int x, int y, int w, int h) {
94 SkRect new_clip; 113 SkRect new_clip;
95 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), 114 new_clip.set(SkIntToScalar(x), SkIntToScalar(y),
96 SkIntToScalar(x + w), SkIntToScalar(y + h)); 115 SkIntToScalar(x + w), SkIntToScalar(y + h));
97 return clipRect(new_clip); 116 return canvas_->clipRect(new_clip);
98 } 117 }
99 118
100 void CanvasSkia::TranslateInt(int x, int y) { 119 void CanvasSkia::TranslateInt(int x, int y) {
101 translate(SkIntToScalar(x), SkIntToScalar(y)); 120 canvas_->translate(SkIntToScalar(x), SkIntToScalar(y));
102 } 121 }
103 122
104 void CanvasSkia::ScaleInt(int x, int y) { 123 void CanvasSkia::ScaleInt(int x, int y) {
105 scale(SkIntToScalar(x), SkIntToScalar(y)); 124 canvas_->scale(SkIntToScalar(x), SkIntToScalar(y));
106 } 125 }
107 126
108 void CanvasSkia::FillRectInt(const SkColor& color, int x, int y, int w, int h) { 127 void CanvasSkia::FillRectInt(const SkColor& color, int x, int y, int w, int h) {
109 FillRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode); 128 FillRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode);
110 } 129 }
111 130
112 void CanvasSkia::FillRectInt(const SkColor& color, 131 void CanvasSkia::FillRectInt(const SkColor& color,
113 int x, int y, int w, int h, 132 int x, int y, int w, int h,
114 SkXfermode::Mode mode) { 133 SkXfermode::Mode mode) {
115 SkPaint paint; 134 SkPaint paint;
(...skipping 26 matching lines...) Expand all
142 // we set a stroke width of 1, for example, this will internally create a 161 // 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. 162 // path and fill it, which causes problems near the edge of the canvas.
144 paint.setStrokeWidth(SkIntToScalar(0)); 163 paint.setStrokeWidth(SkIntToScalar(0));
145 paint.setXfermodeMode(mode); 164 paint.setXfermodeMode(mode);
146 165
147 DrawRectInt(x, y, w, h, paint); 166 DrawRectInt(x, y, w, h, paint);
148 } 167 }
149 168
150 void CanvasSkia::DrawRectInt(int x, int y, int w, int h, const SkPaint& paint) { 169 void CanvasSkia::DrawRectInt(int x, int y, int w, int h, const SkPaint& paint) {
151 SkIRect rc = { x, y, x + w, y + h }; 170 SkIRect rc = { x, y, x + w, y + h };
152 drawIRect(rc, paint); 171 canvas_->drawIRect(rc, paint);
153 } 172 }
154 173
155 void CanvasSkia::DrawLineInt(const SkColor& color, 174 void CanvasSkia::DrawLineInt(const SkColor& color,
156 int x1, int y1, 175 int x1, int y1,
157 int x2, int y2) { 176 int x2, int y2) {
158 SkPaint paint; 177 SkPaint paint;
159 paint.setColor(color); 178 paint.setColor(color);
160 paint.setStrokeWidth(SkIntToScalar(1)); 179 paint.setStrokeWidth(SkIntToScalar(1));
161 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), 180 canvas_->drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2),
162 SkIntToScalar(y2), paint); 181 SkIntToScalar(y2), paint);
163 } 182 }
164 183
165 void CanvasSkia::DrawFocusRect(int x, int y, int width, int height) { 184 void CanvasSkia::DrawFocusRect(int x, int y, int width, int height) {
166 // Create a 2D bitmap containing alternating on/off pixels - we do this 185 // 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 186 // 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 187 // 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). 188 // have a dot pattern out of phase to each other).
170 static SkBitmap* dots = NULL; 189 static SkBitmap* dots = NULL;
171 if (!dots) { 190 if (!dots) {
172 int col_pixels = 32; 191 int col_pixels = 32;
(...skipping 27 matching lines...) Expand all
200 paint.setShader(shader); 219 paint.setShader(shader);
201 shader->unref(); 220 shader->unref();
202 221
203 DrawRectInt(x, y, width, 1, paint); 222 DrawRectInt(x, y, width, 1, paint);
204 DrawRectInt(x, y + height - 1, width, 1, paint); 223 DrawRectInt(x, y + height - 1, width, 1, paint);
205 DrawRectInt(x, y, 1, height, paint); 224 DrawRectInt(x, y, 1, height, paint);
206 DrawRectInt(x + width - 1, y, 1, height, paint); 225 DrawRectInt(x + width - 1, y, 1, height, paint);
207 } 226 }
208 227
209 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { 228 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) {
210 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); 229 canvas_->drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y));
211 } 230 }
212 231
213 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, 232 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap,
214 int x, int y, 233 int x, int y,
215 const SkPaint& paint) { 234 const SkPaint& paint) {
216 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); 235 canvas_->drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
217 } 236 }
218 237
219 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, 238 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap,
220 int src_x, int src_y, int src_w, int src_h, 239 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, 240 int dest_x, int dest_y, int dest_w, int dest_h,
222 bool filter) { 241 bool filter) {
223 SkPaint p; 242 SkPaint p;
224 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, 243 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y,
225 dest_w, dest_h, filter, p); 244 dest_w, dest_h, filter, p);
226 } 245 }
(...skipping 15 matching lines...) Expand all
242 261
243 SkRect dest_rect = { SkIntToScalar(dest_x), 262 SkRect dest_rect = { SkIntToScalar(dest_x),
244 SkIntToScalar(dest_y), 263 SkIntToScalar(dest_y),
245 SkIntToScalar(dest_x + dest_w), 264 SkIntToScalar(dest_x + dest_w),
246 SkIntToScalar(dest_y + dest_h) }; 265 SkIntToScalar(dest_y + dest_h) };
247 266
248 if (src_w == dest_w && src_h == dest_h) { 267 if (src_w == dest_w && src_h == dest_h) {
249 // Workaround for apparent bug in Skia that causes image to occasionally 268 // Workaround for apparent bug in Skia that causes image to occasionally
250 // shift. 269 // shift.
251 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; 270 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
252 drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); 271 canvas_->drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
253 return; 272 return;
254 } 273 }
255 274
256 // Make a bitmap shader that contains the bitmap we want to draw. This is 275 // 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 276 // 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 277 // more control over quality and will use the mipmap in the source image if
259 // it has one, whereas drawBitmap won't. 278 // it has one, whereas drawBitmap won't.
260 SkShader* shader = SkShader::CreateBitmapShader(bitmap, 279 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
261 SkShader::kRepeat_TileMode, 280 SkShader::kRepeat_TileMode,
262 SkShader::kRepeat_TileMode); 281 SkShader::kRepeat_TileMode);
263 SkMatrix shader_scale; 282 SkMatrix shader_scale;
264 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), 283 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w),
265 SkFloatToScalar(static_cast<float>(dest_h) / src_h)); 284 SkFloatToScalar(static_cast<float>(dest_h) / src_h));
266 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); 285 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
267 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); 286 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
268 shader->setLocalMatrix(shader_scale); 287 shader->setLocalMatrix(shader_scale);
269 288
270 // Set up our paint to use the shader & release our reference (now just owned 289 // Set up our paint to use the shader & release our reference (now just owned
271 // by the paint). 290 // by the paint).
272 SkPaint p(paint); 291 SkPaint p(paint);
273 p.setFilterBitmap(filter); 292 p.setFilterBitmap(filter);
274 p.setShader(shader); 293 p.setShader(shader);
275 shader->unref(); 294 shader->unref();
276 295
277 // The rect will be filled by the bitmap. 296 // The rect will be filled by the bitmap.
278 drawRect(dest_rect, p); 297 canvas_->drawRect(dest_rect, p);
279 } 298 }
280 299
281 void CanvasSkia::DrawStringInt(const string16& text, 300 void CanvasSkia::DrawStringInt(const string16& text,
282 const gfx::Font& font, 301 const gfx::Font& font,
283 const SkColor& color, 302 const SkColor& color,
284 int x, int y, int w, int h) { 303 int x, int y, int w, int h) {
285 DrawStringInt(text, font, color, x, y, w, h, 304 DrawStringInt(text, font, color, x, y, w, h,
286 gfx::CanvasSkia::DefaultCanvasTextAlignment()); 305 gfx::CanvasSkia::DefaultCanvasTextAlignment());
287 } 306 }
288 307
(...skipping 20 matching lines...) Expand all
309 328
310 SkShader* shader = SkShader::CreateBitmapShader(bitmap, 329 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
311 SkShader::kRepeat_TileMode, 330 SkShader::kRepeat_TileMode,
312 SkShader::kRepeat_TileMode); 331 SkShader::kRepeat_TileMode);
313 paint.setShader(shader); 332 paint.setShader(shader);
314 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); 333 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
315 334
316 // CreateBitmapShader returns a Shader with a reference count of one, we 335 // CreateBitmapShader returns a Shader with a reference count of one, we
317 // need to unref after paint takes ownership of the shader. 336 // need to unref after paint takes ownership of the shader.
318 shader->unref(); 337 shader->unref();
319 save(); 338 canvas_->save();
320 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); 339 canvas_->translate(SkIntToScalar(dest_x - src_x),
340 SkIntToScalar(dest_y - src_y));
321 ClipRectInt(src_x, src_y, w, h); 341 ClipRectInt(src_x, src_y, w, h);
322 drawPaint(paint); 342 canvas_->drawPaint(paint);
323 restore(); 343 canvas_->restore();
324 } 344 }
325 345
326 gfx::NativeDrawingContext CanvasSkia::BeginPlatformPaint() { 346 gfx::NativeDrawingContext CanvasSkia::BeginPlatformPaint() {
327 return skia::BeginPlatformPaint(this); 347 return skia::BeginPlatformPaint(canvas_);
328 } 348 }
329 349
330 void CanvasSkia::EndPlatformPaint() { 350 void CanvasSkia::EndPlatformPaint() {
331 skia::EndPlatformPaint(this); 351 skia::EndPlatformPaint(canvas_);
332 } 352 }
333 353
334 #if !defined(OS_MACOSX) 354 #if !defined(OS_MACOSX)
335 void CanvasSkia::Transform(const ui::Transform& transform) { 355 void CanvasSkia::Transform(const ui::Transform& transform) {
336 concat(transform.matrix()); 356 canvas_->concat(transform.matrix());
337 } 357 }
338 #endif 358 #endif
339 359
340 CanvasSkia* CanvasSkia::AsCanvasSkia() { 360 CanvasSkia* CanvasSkia::AsCanvasSkia() {
341 return this; 361 return this;
342 } 362 }
343 363
344 const CanvasSkia* CanvasSkia::AsCanvasSkia() const { 364 const CanvasSkia* CanvasSkia::AsCanvasSkia() const {
345 return this; 365 return this;
346 } 366 }
347 367
368 SkCanvas* CanvasSkia::GetSkCanvas() {
369 return canvas_;
370 }
371
372 const SkCanvas* CanvasSkia::GetSkCanvas() const {
373 return canvas_;
374 }
375
348 //////////////////////////////////////////////////////////////////////////////// 376 ////////////////////////////////////////////////////////////////////////////////
349 // CanvasSkia, private: 377 // CanvasSkia, private:
350 378
351 bool CanvasSkia::IntersectsClipRectInt(int x, int y, int w, int h) { 379 bool CanvasSkia::IntersectsClipRectInt(int x, int y, int w, int h) {
352 SkRect clip; 380 SkRect clip;
353 return getClipBounds(&clip) && 381 return canvas_->getClipBounds(&clip) &&
354 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), 382 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
355 SkIntToScalar(y + h)); 383 SkIntToScalar(y + h));
356 } 384 }
357 385
358 //////////////////////////////////////////////////////////////////////////////// 386 ////////////////////////////////////////////////////////////////////////////////
359 // Canvas, public: 387 // Canvas, public:
360 388
361 Canvas* Canvas::CreateCanvas() { 389 Canvas* Canvas::CreateCanvas() {
362 return new CanvasSkia; 390 return new CanvasSkia;
363 } 391 }
(...skipping 25 matching lines...) Expand all
389 417
390 CanvasPaint* CanvasPaint::CreateCanvasPaint(gfx::NativeView view) { 418 CanvasPaint* CanvasPaint::CreateCanvasPaint(gfx::NativeView view) {
391 #if defined(OS_WIN) && !defined(USE_AURA) 419 #if defined(OS_WIN) && !defined(USE_AURA)
392 return new CanvasPaintWin(view); 420 return new CanvasPaintWin(view);
393 #else 421 #else
394 return NULL; 422 return NULL;
395 #endif 423 #endif
396 } 424 }
397 425
398 } // namespace gfx 426 } // namespace gfx
OLDNEW
« ui/gfx/canvas.h ('K') | « ui/gfx/canvas_skia.h ('k') | ui/gfx/canvas_skia_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698