| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 SKY_ENGINE_CORE_PAINTING_CANVAS_H_ | 5 #ifndef SKY_ENGINE_CORE_PAINTING_CANVAS_H_ |
| 6 #define SKY_ENGINE_CORE_PAINTING_CANVAS_H_ | 6 #define SKY_ENGINE_CORE_PAINTING_CANVAS_H_ |
| 7 | 7 |
| 8 #include "sky/engine/bindings/exception_state.h" | 8 #include "sky/engine/bindings/exception_state.h" |
| 9 #include "sky/engine/core/painting/CanvasPath.h" | 9 #include "sky/engine/core/painting/CanvasPath.h" |
| 10 #include "sky/engine/core/painting/Offset.h" |
| 10 #include "sky/engine/core/painting/Paint.h" | 11 #include "sky/engine/core/painting/Paint.h" |
| 11 #include "sky/engine/core/painting/Picture.h" | 12 #include "sky/engine/core/painting/Picture.h" |
| 12 #include "sky/engine/core/painting/PictureRecorder.h" | 13 #include "sky/engine/core/painting/PictureRecorder.h" |
| 14 #include "sky/engine/core/painting/Point.h" |
| 13 #include "sky/engine/core/painting/RRect.h" | 15 #include "sky/engine/core/painting/RRect.h" |
| 14 #include "sky/engine/core/painting/Rect.h" | 16 #include "sky/engine/core/painting/Rect.h" |
| 17 #include "sky/engine/core/painting/Size.h" |
| 15 #include "sky/engine/platform/graphics/DisplayList.h" | 18 #include "sky/engine/platform/graphics/DisplayList.h" |
| 16 #include "sky/engine/tonic/dart_wrappable.h" | 19 #include "sky/engine/tonic/dart_wrappable.h" |
| 17 #include "sky/engine/tonic/float32_list.h" | 20 #include "sky/engine/tonic/float32_list.h" |
| 18 #include "sky/engine/wtf/PassRefPtr.h" | 21 #include "sky/engine/wtf/PassRefPtr.h" |
| 19 #include "sky/engine/wtf/RefCounted.h" | 22 #include "sky/engine/wtf/RefCounted.h" |
| 20 #include "third_party/skia/include/core/SkCanvas.h" | 23 #include "third_party/skia/include/core/SkCanvas.h" |
| 21 | 24 |
| 22 namespace blink { | 25 namespace blink { |
| 23 class CanvasImage; | 26 class CanvasImage; |
| 24 | 27 |
| 25 class Canvas : public RefCounted<Canvas>, public DartWrappable { | 28 class Canvas : public RefCounted<Canvas>, public DartWrappable { |
| 26 DEFINE_WRAPPERTYPEINFO(); | 29 DEFINE_WRAPPERTYPEINFO(); |
| 27 public: | 30 public: |
| 28 static PassRefPtr<Canvas> create(SkCanvas* skCanvas) { | 31 static PassRefPtr<Canvas> create(SkCanvas* skCanvas) { |
| 29 ASSERT(skCanvas); | 32 ASSERT(skCanvas); |
| 30 return adoptRef(new Canvas(skCanvas)); | 33 return adoptRef(new Canvas(skCanvas)); |
| 31 } | 34 } |
| 32 | 35 |
| 33 static PassRefPtr<Canvas> create(PictureRecorder* recorder, | 36 static PassRefPtr<Canvas> create(PictureRecorder* recorder, |
| 34 double width, | 37 Size& bounds, |
| 35 double height, | |
| 36 ExceptionState& es) { | 38 ExceptionState& es) { |
| 37 ASSERT(recorder); | 39 ASSERT(recorder); |
| 38 if (recorder->isRecording()) { | 40 if (recorder->isRecording()) { |
| 39 es.ThrowTypeError( | 41 es.ThrowTypeError( |
| 40 "You must call PictureRecorder.endRecording() before reusing a" | 42 "You must call PictureRecorder.endRecording() before reusing a" |
| 41 " PictureRecorder to create a new Canvas object."); | 43 " PictureRecorder to create a new Canvas object."); |
| 42 // TODO(iansf): We should return a nullptr here, I think, but doing | 44 // TODO(iansf): We should return a nullptr here, I think, but doing |
| 43 // so will require modifying the dart template code to | 45 // so will require modifying the dart template code to |
| 44 // to correctly handle constructors that throw | 46 // to correctly handle constructors that throw |
| 45 // exceptions. For now, just let it return a dart | 47 // exceptions. For now, just let it return a dart |
| 46 // object that may cause a crash later on -- if the | 48 // object that may cause a crash later on -- if the |
| 47 // dart code catches the error, it will leak a canvas | 49 // dart code catches the error, it will leak a canvas |
| 48 // but it won't crash. | 50 // but it won't crash. |
| 49 } | 51 } |
| 50 PassRefPtr<Canvas> canvas = create( | 52 PassRefPtr<Canvas> canvas = create( |
| 51 recorder->beginRecording(width, height)); | 53 recorder->beginRecording(bounds.sk_size.width(), bounds.sk_size.height
())); |
| 52 recorder->set_canvas(canvas.get()); | 54 recorder->set_canvas(canvas.get()); |
| 53 return canvas; | 55 return canvas; |
| 54 } | 56 } |
| 55 | 57 |
| 56 ~Canvas() override; | 58 ~Canvas() override; |
| 57 | 59 |
| 58 void save(); | 60 void save(); |
| 59 void saveLayer(const Rect& bounds, const Paint* paint = nullptr); | 61 void saveLayer(const Rect& bounds, const Paint* paint = nullptr); |
| 60 void restore(); | 62 void restore(); |
| 61 | 63 |
| 62 void translate(float dx, float dy); | 64 void translate(float dx, float dy); |
| 63 void scale(float sx, float sy); | 65 void scale(float sx, float sy); |
| 64 void rotate(float radians); | 66 void rotate(float radians); |
| 65 void skew(float sx, float sy); | 67 void skew(float sx, float sy); |
| 66 void concat(const Float32List& matrix4); | 68 void concat(const Float32List& matrix4); |
| 67 | 69 |
| 68 void clipRect(const Rect& rect); | 70 void clipRect(const Rect& rect); |
| 69 void clipRRect(const RRect* rrect); | 71 void clipRRect(const RRect* rrect); |
| 70 void clipPath(const CanvasPath* path); | 72 void clipPath(const CanvasPath* path); |
| 71 | 73 |
| 72 void drawLine(float x0, float y0, float x1, float y1, const Paint* paint); | 74 void drawLine(const Point& p1, const Point& p2, const Paint* paint); |
| 73 void drawPicture(Picture* picture); | 75 void drawPicture(Picture* picture); |
| 74 void drawPaint(const Paint* paint); | 76 void drawPaint(const Paint* paint); |
| 75 void drawRect(const Rect& rect, const Paint* paint); | 77 void drawRect(const Rect& rect, const Paint* paint); |
| 76 void drawRRect(const RRect* rrect, const Paint* paint); | 78 void drawRRect(const RRect* rrect, const Paint* paint); |
| 77 void drawOval(const Rect& rect, const Paint* paint); | 79 void drawOval(const Rect& rect, const Paint* paint); |
| 78 void drawCircle(float x, float y, float radius, const Paint* paint); | 80 void drawCircle(const Point& c, float radius, const Paint* paint); |
| 79 void drawPath(const CanvasPath* path, const Paint* paint); | 81 void drawPath(const CanvasPath* path, const Paint* paint); |
| 80 void drawImage(const CanvasImage* image, | 82 void drawImage(const CanvasImage* image, const Point& p, const Paint* paint)
; |
| 81 float x, | |
| 82 float y, | |
| 83 const Paint* paint); | |
| 84 void drawImageRect(const CanvasImage* image, Rect& src, Rect& dst, Paint* pa
int); | 83 void drawImageRect(const CanvasImage* image, Rect& src, Rect& dst, Paint* pa
int); |
| 85 | 84 |
| 86 SkCanvas* skCanvas() { return m_canvas; } | 85 SkCanvas* skCanvas() { return m_canvas; } |
| 87 void clearSkCanvas() { m_canvas = nullptr; } | 86 void clearSkCanvas() { m_canvas = nullptr; } |
| 88 bool isRecording() const { return !!m_canvas; } | 87 bool isRecording() const { return !!m_canvas; } |
| 89 | 88 |
| 90 protected: | 89 protected: |
| 91 explicit Canvas(SkCanvas* skCanvas); | 90 explicit Canvas(SkCanvas* skCanvas); |
| 92 | 91 |
| 93 private: | 92 private: |
| 94 // The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording, | 93 // The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording, |
| 95 // which does not transfer ownership. For this reason, we hold a raw | 94 // which does not transfer ownership. For this reason, we hold a raw |
| 96 // pointer and manually set the SkCanvas to null in clearSkCanvas. | 95 // pointer and manually set the SkCanvas to null in clearSkCanvas. |
| 97 SkCanvas* m_canvas; | 96 SkCanvas* m_canvas; |
| 98 }; | 97 }; |
| 99 | 98 |
| 100 } // namespace blink | 99 } // namespace blink |
| 101 | 100 |
| 102 #endif // SKY_ENGINE_CORE_PAINTING_CANVAS_H_ | 101 #endif // SKY_ENGINE_CORE_PAINTING_CANVAS_H_ |
| OLD | NEW |