OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef BaseRenderingContext2D_h | 5 #ifndef BaseRenderingContext2D_h |
6 #define BaseRenderingContext2D_h | 6 #define BaseRenderingContext2D_h |
7 | 7 |
8 #include "bindings/modules/v8/CSSImageValueOrHTMLImageElementOrHTMLVideoElementO
rHTMLCanvasElementOrImageBitmapOrOffscreenCanvas.h" | 8 #include "bindings/modules/v8/CSSImageValueOrHTMLImageElementOrHTMLVideoElementO
rHTMLCanvasElementOrImageBitmapOrOffscreenCanvas.h" |
9 #include "bindings/modules/v8/StringOrCanvasGradientOrCanvasPattern.h" | 9 #include "bindings/modules/v8/StringOrCanvasGradientOrCanvasPattern.h" |
10 #include "core/html/ImageData.h" | 10 #include "core/html/ImageData.h" |
11 #include "modules/ModulesExport.h" | 11 #include "modules/ModulesExport.h" |
12 #include "modules/canvas2d/CanvasGradient.h" | 12 #include "modules/canvas2d/CanvasGradient.h" |
13 #include "modules/canvas2d/CanvasPathMethods.h" | 13 #include "modules/canvas2d/CanvasPathMethods.h" |
14 #include "modules/canvas2d/CanvasRenderingContext2DState.h" | 14 #include "modules/canvas2d/CanvasRenderingContext2DState.h" |
15 #include "modules/canvas2d/CanvasStyle.h" | 15 #include "modules/canvas2d/CanvasStyle.h" |
16 #include "platform/graphics/ColorBehavior.h" | 16 #include "platform/graphics/ColorBehavior.h" |
17 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h" | 17 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h" |
18 #include "third_party/skia/include/core/SkCanvas.h" | 18 #include "platform/graphics/paint/PaintCanvas.h" |
19 #include "third_party/skia/include/effects/SkComposeImageFilter.h" | 19 #include "third_party/skia/include/effects/SkComposeImageFilter.h" |
20 | 20 |
21 namespace blink { | 21 namespace blink { |
22 | 22 |
23 class CanvasImageSource; | 23 class CanvasImageSource; |
24 class Color; | 24 class Color; |
25 class Image; | 25 class Image; |
26 class ImageBuffer; | 26 class ImageBuffer; |
27 class Path2D; | 27 class Path2D; |
28 class SVGMatrixTearOff; | 28 class SVGMatrixTearOff; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 215 |
216 virtual int width() const = 0; | 216 virtual int width() const = 0; |
217 virtual int height() const = 0; | 217 virtual int height() const = 0; |
218 | 218 |
219 virtual bool hasImageBuffer() const = 0; | 219 virtual bool hasImageBuffer() const = 0; |
220 virtual ImageBuffer* imageBuffer() const = 0; | 220 virtual ImageBuffer* imageBuffer() const = 0; |
221 | 221 |
222 virtual bool parseColorOrCurrentColor(Color&, | 222 virtual bool parseColorOrCurrentColor(Color&, |
223 const String& colorString) const = 0; | 223 const String& colorString) const = 0; |
224 | 224 |
225 virtual SkCanvas* drawingCanvas() const = 0; | 225 virtual PaintCanvas* drawingCanvas() const = 0; |
226 virtual SkCanvas* existingDrawingCanvas() const = 0; | 226 virtual PaintCanvas* existingDrawingCanvas() const = 0; |
227 virtual void disableDeferral(DisableDeferralReason) = 0; | 227 virtual void disableDeferral(DisableDeferralReason) = 0; |
228 | 228 |
229 virtual AffineTransform baseTransform() const = 0; | 229 virtual AffineTransform baseTransform() const = 0; |
230 | 230 |
231 virtual void didDraw(const SkIRect& dirtyRect) = 0; | 231 virtual void didDraw(const SkIRect& dirtyRect) = 0; |
232 | 232 |
233 virtual bool stateHasFilter() = 0; | 233 virtual bool stateHasFilter() = 0; |
234 virtual sk_sp<SkImageFilter> stateGetFilter() = 0; | 234 virtual sk_sp<SkImageFilter> stateGetFilter() = 0; |
235 virtual void snapshotStateForFilter() = 0; | 235 virtual void snapshotStateForFilter() = 0; |
236 | 236 |
237 virtual void validateStateStack() const = 0; | 237 virtual void validateStateStack() const = 0; |
238 | 238 |
239 virtual bool hasAlpha() const = 0; | 239 virtual bool hasAlpha() const = 0; |
240 | 240 |
241 virtual bool isContextLost() const = 0; | 241 virtual bool isContextLost() const = 0; |
242 | 242 |
243 virtual ColorBehavior drawImageColorBehavior() const = 0; | 243 virtual ColorBehavior drawImageColorBehavior() const = 0; |
244 | 244 |
245 virtual void willDrawImage(CanvasImageSource*) const {} | 245 virtual void willDrawImage(CanvasImageSource*) const {} |
246 | 246 |
247 void restoreMatrixClipStack(SkCanvas*) const; | 247 void restoreMatrixClipStack(PaintCanvas*) const; |
248 | 248 |
249 DECLARE_VIRTUAL_TRACE(); | 249 DECLARE_VIRTUAL_TRACE(); |
250 | 250 |
251 enum DrawCallType { | 251 enum DrawCallType { |
252 StrokePath = 0, | 252 StrokePath = 0, |
253 FillPath, | 253 FillPath, |
254 DrawVectorImage, | 254 DrawVectorImage, |
255 DrawBitmapImage, | 255 DrawBitmapImage, |
256 FillText, | 256 FillText, |
257 StrokeText, | 257 StrokeText, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 void inflateStrokeRect(FloatRect&) const; | 320 void inflateStrokeRect(FloatRect&) const; |
321 | 321 |
322 void unwindStateStack(); | 322 void unwindStateStack(); |
323 | 323 |
324 enum DrawType { | 324 enum DrawType { |
325 ClipFill, // Fill that is already known to cover the current clip | 325 ClipFill, // Fill that is already known to cover the current clip |
326 UntransformedUnclippedFill | 326 UntransformedUnclippedFill |
327 }; | 327 }; |
328 | 328 |
329 void checkOverdraw(const SkRect&, | 329 void checkOverdraw(const SkRect&, |
330 const SkPaint*, | 330 const PaintFlags*, |
331 CanvasRenderingContext2DState::ImageType, | 331 CanvasRenderingContext2DState::ImageType, |
332 DrawType); | 332 DrawType); |
333 | 333 |
334 HeapVector<Member<CanvasRenderingContext2DState>> m_stateStack; | 334 HeapVector<Member<CanvasRenderingContext2DState>> m_stateStack; |
335 AntiAliasingMode m_clipAntialiasing; | 335 AntiAliasingMode m_clipAntialiasing; |
336 | 336 |
337 void trackDrawCall(DrawCallType, | 337 void trackDrawCall(DrawCallType, |
338 Path2D* path2d = nullptr, | 338 Path2D* path2d = nullptr, |
339 int width = 0, | 339 int width = 0, |
340 int height = 0); | 340 int height = 0); |
341 | 341 |
342 mutable UsageCounters m_usageCounters; | 342 mutable UsageCounters m_usageCounters; |
343 | 343 |
344 float estimateRenderingCost( | 344 float estimateRenderingCost( |
345 ExpensiveCanvasHeuristicParameters::RenderingModeCostIndex) const; | 345 ExpensiveCanvasHeuristicParameters::RenderingModeCostIndex) const; |
346 | 346 |
347 private: | 347 private: |
348 void realizeSaves(); | 348 void realizeSaves(); |
349 | 349 |
350 bool shouldDrawImageAntialiased(const FloatRect& destRect) const; | 350 bool shouldDrawImageAntialiased(const FloatRect& destRect) const; |
351 | 351 |
352 void drawPathInternal(const Path&, | 352 void drawPathInternal(const Path&, |
353 CanvasRenderingContext2DState::PaintType, | 353 CanvasRenderingContext2DState::PaintType, |
354 SkPath::FillType = SkPath::kWinding_FillType); | 354 SkPath::FillType = SkPath::kWinding_FillType); |
355 void drawImageInternal(SkCanvas*, | 355 void drawImageInternal(PaintCanvas*, |
356 CanvasImageSource*, | 356 CanvasImageSource*, |
357 Image*, | 357 Image*, |
358 const FloatRect& srcRect, | 358 const FloatRect& srcRect, |
359 const FloatRect& dstRect, | 359 const FloatRect& dstRect, |
360 const SkPaint*); | 360 const PaintFlags*); |
361 void clipInternal(const Path&, const String& windingRuleString); | 361 void clipInternal(const Path&, const String& windingRuleString); |
362 | 362 |
363 bool isPointInPathInternal(const Path&, | 363 bool isPointInPathInternal(const Path&, |
364 const double x, | 364 const double x, |
365 const double y, | 365 const double y, |
366 const String& windingRuleString); | 366 const String& windingRuleString); |
367 bool isPointInStrokeInternal(const Path&, const double x, const double y); | 367 bool isPointInStrokeInternal(const Path&, const double x, const double y); |
368 | 368 |
369 static bool isFullCanvasCompositeMode(SkBlendMode); | 369 static bool isFullCanvasCompositeMode(SkBlendMode); |
370 | 370 |
371 template <typename DrawFunc> | 371 template <typename DrawFunc> |
372 void compositedDraw(const DrawFunc&, | 372 void compositedDraw(const DrawFunc&, |
373 SkCanvas*, | 373 PaintCanvas*, |
374 CanvasRenderingContext2DState::PaintType, | 374 CanvasRenderingContext2DState::PaintType, |
375 CanvasRenderingContext2DState::ImageType); | 375 CanvasRenderingContext2DState::ImageType); |
376 | 376 |
377 void clearCanvas(); | 377 void clearCanvas(); |
378 bool rectContainsTransformedRect(const FloatRect&, const SkIRect&) const; | 378 bool rectContainsTransformedRect(const FloatRect&, const SkIRect&) const; |
379 }; | 379 }; |
380 | 380 |
381 template <typename DrawFunc, typename ContainsFunc> | 381 template <typename DrawFunc, typename ContainsFunc> |
382 bool BaseRenderingContext2D::draw( | 382 bool BaseRenderingContext2D::draw( |
383 const DrawFunc& drawFunc, | 383 const DrawFunc& drawFunc, |
(...skipping 15 matching lines...) Expand all Loading... |
399 if (gradient && gradient->getGradient()->isZeroSize()) | 399 if (gradient && gradient->getGradient()->isZeroSize()) |
400 return false; | 400 return false; |
401 } | 401 } |
402 | 402 |
403 if (isFullCanvasCompositeMode(state().globalComposite()) || | 403 if (isFullCanvasCompositeMode(state().globalComposite()) || |
404 stateHasFilter()) { | 404 stateHasFilter()) { |
405 compositedDraw(drawFunc, drawingCanvas(), paintType, imageType); | 405 compositedDraw(drawFunc, drawingCanvas(), paintType, imageType); |
406 didDraw(clipBounds); | 406 didDraw(clipBounds); |
407 } else if (state().globalComposite() == SkBlendMode::kSrc) { | 407 } else if (state().globalComposite() == SkBlendMode::kSrc) { |
408 clearCanvas(); // takes care of checkOverdraw() | 408 clearCanvas(); // takes care of checkOverdraw() |
409 const SkPaint* paint = | 409 const PaintFlags* paint = |
410 state().getPaint(paintType, DrawForegroundOnly, imageType); | 410 state().getPaint(paintType, DrawForegroundOnly, imageType); |
411 drawFunc(drawingCanvas(), paint); | 411 drawFunc(drawingCanvas(), paint); |
412 didDraw(clipBounds); | 412 didDraw(clipBounds); |
413 } else { | 413 } else { |
414 SkIRect dirtyRect; | 414 SkIRect dirtyRect; |
415 if (computeDirtyRect(bounds, clipBounds, &dirtyRect)) { | 415 if (computeDirtyRect(bounds, clipBounds, &dirtyRect)) { |
416 const SkPaint* paint = | 416 const PaintFlags* paint = |
417 state().getPaint(paintType, DrawShadowAndForeground, imageType); | 417 state().getPaint(paintType, DrawShadowAndForeground, imageType); |
418 if (paintType != CanvasRenderingContext2DState::StrokePaintType && | 418 if (paintType != CanvasRenderingContext2DState::StrokePaintType && |
419 drawCoversClipBounds(clipBounds)) | 419 drawCoversClipBounds(clipBounds)) |
420 checkOverdraw(bounds, paint, imageType, ClipFill); | 420 checkOverdraw(bounds, paint, imageType, ClipFill); |
421 drawFunc(drawingCanvas(), paint); | 421 drawFunc(drawingCanvas(), paint); |
422 didDraw(dirtyRect); | 422 didDraw(dirtyRect); |
423 } | 423 } |
424 } | 424 } |
425 return true; | 425 return true; |
426 } | 426 } |
427 | 427 |
428 template <typename DrawFunc> | 428 template <typename DrawFunc> |
429 void BaseRenderingContext2D::compositedDraw( | 429 void BaseRenderingContext2D::compositedDraw( |
430 const DrawFunc& drawFunc, | 430 const DrawFunc& drawFunc, |
431 SkCanvas* c, | 431 PaintCanvas* c, |
432 CanvasRenderingContext2DState::PaintType paintType, | 432 CanvasRenderingContext2DState::PaintType paintType, |
433 CanvasRenderingContext2DState::ImageType imageType) { | 433 CanvasRenderingContext2DState::ImageType imageType) { |
434 sk_sp<SkImageFilter> filter = stateGetFilter(); | 434 sk_sp<SkImageFilter> filter = stateGetFilter(); |
435 ASSERT(isFullCanvasCompositeMode(state().globalComposite()) || filter); | 435 ASSERT(isFullCanvasCompositeMode(state().globalComposite()) || filter); |
436 SkMatrix ctm = c->getTotalMatrix(); | 436 SkMatrix ctm = c->getTotalMatrix(); |
437 c->resetMatrix(); | 437 c->resetMatrix(); |
438 SkPaint compositePaint; | 438 PaintFlags compositePaint; |
439 compositePaint.setBlendMode((SkBlendMode)state().globalComposite()); | 439 compositePaint.setBlendMode((SkBlendMode)state().globalComposite()); |
440 if (state().shouldDrawShadows()) { | 440 if (state().shouldDrawShadows()) { |
441 // unroll into two independently composited passes if drawing shadows | 441 // unroll into two independently composited passes if drawing shadows |
442 SkPaint shadowPaint = | 442 PaintFlags shadowPaint = |
443 *state().getPaint(paintType, DrawShadowOnly, imageType); | 443 *state().getPaint(paintType, DrawShadowOnly, imageType); |
444 int saveCount = c->getSaveCount(); | 444 int saveCount = c->getSaveCount(); |
445 if (filter) { | 445 if (filter) { |
446 SkPaint foregroundPaint = | 446 PaintFlags foregroundPaint = |
447 *state().getPaint(paintType, DrawForegroundOnly, imageType); | 447 *state().getPaint(paintType, DrawForegroundOnly, imageType); |
448 foregroundPaint.setImageFilter(SkComposeImageFilter::Make( | 448 foregroundPaint.setImageFilter(SkComposeImageFilter::Make( |
449 SkComposeImageFilter::Make(foregroundPaint.refImageFilter(), | 449 SkComposeImageFilter::Make(foregroundPaint.refImageFilter(), |
450 shadowPaint.refImageFilter()), | 450 shadowPaint.refImageFilter()), |
451 filter)); | 451 filter)); |
452 c->setMatrix(ctm); | 452 c->setMatrix(ctm); |
453 drawFunc(c, &foregroundPaint); | 453 drawFunc(c, &foregroundPaint); |
454 } else { | 454 } else { |
455 ASSERT(isFullCanvasCompositeMode(state().globalComposite())); | 455 ASSERT(isFullCanvasCompositeMode(state().globalComposite())); |
456 c->saveLayer(nullptr, &compositePaint); | 456 c->saveLayer(nullptr, &compositePaint); |
457 shadowPaint.setBlendMode(SkBlendMode::kSrcOver); | 457 shadowPaint.setBlendMode(SkBlendMode::kSrcOver); |
458 c->setMatrix(ctm); | 458 c->setMatrix(ctm); |
459 drawFunc(c, &shadowPaint); | 459 drawFunc(c, &shadowPaint); |
460 } | 460 } |
461 c->restoreToCount(saveCount); | 461 c->restoreToCount(saveCount); |
462 } | 462 } |
463 | 463 |
464 compositePaint.setImageFilter(std::move(filter)); | 464 compositePaint.setImageFilter(std::move(filter)); |
465 c->saveLayer(nullptr, &compositePaint); | 465 c->saveLayer(nullptr, &compositePaint); |
466 SkPaint foregroundPaint = | 466 PaintFlags foregroundPaint = |
467 *state().getPaint(paintType, DrawForegroundOnly, imageType); | 467 *state().getPaint(paintType, DrawForegroundOnly, imageType); |
468 foregroundPaint.setBlendMode(SkBlendMode::kSrcOver); | 468 foregroundPaint.setBlendMode(SkBlendMode::kSrcOver); |
469 c->setMatrix(ctm); | 469 c->setMatrix(ctm); |
470 drawFunc(c, &foregroundPaint); | 470 drawFunc(c, &foregroundPaint); |
471 c->restore(); | 471 c->restore(); |
472 c->setMatrix(ctm); | 472 c->setMatrix(ctm); |
473 } | 473 } |
474 | 474 |
475 } // namespace blink | 475 } // namespace blink |
476 | 476 |
477 #endif // BaseRenderingContext2D_h | 477 #endif // BaseRenderingContext2D_h |
OLD | NEW |