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