OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 10 matching lines...) Expand all Loading... |
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
23 * DAMAGE. | 23 * DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "platform/graphics/GraphicsContext.h" | 26 #include "platform/graphics/GraphicsContext.h" |
27 | 27 |
28 #include "platform/graphics/BitmapImage.h" | 28 #include "platform/graphics/BitmapImage.h" |
29 #include "platform/graphics/Path.h" | 29 #include "platform/graphics/Path.h" |
30 #include "platform/graphics/paint/PaintController.h" | 30 #include "platform/graphics/paint/PaintController.h" |
| 31 #include "platform/graphics/paint/PaintRecord.h" |
31 #include "platform/testing/FontTestHelpers.h" | 32 #include "platform/testing/FontTestHelpers.h" |
32 #include "platform/testing/UnitTestHelpers.h" | 33 #include "platform/testing/UnitTestHelpers.h" |
33 #include "platform/text/TextRun.h" | 34 #include "platform/text/TextRun.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
35 #include "third_party/skia/include/core/SkBitmap.h" | 36 #include "third_party/skia/include/core/SkBitmap.h" |
36 #include "third_party/skia/include/core/SkCanvas.h" | 37 #include "third_party/skia/include/core/SkCanvas.h" |
37 #include "third_party/skia/include/core/SkPicture.h" | |
38 #include "third_party/skia/include/core/SkShader.h" | 38 #include "third_party/skia/include/core/SkShader.h" |
39 #include <memory> | 39 #include <memory> |
40 | 40 |
41 namespace blink { | 41 namespace blink { |
42 | 42 |
43 #define EXPECT_EQ_RECT(a, b) \ | 43 #define EXPECT_EQ_RECT(a, b) \ |
44 EXPECT_EQ(a.x(), b.x()); \ | 44 EXPECT_EQ(a.x(), b.x()); \ |
45 EXPECT_EQ(a.y(), b.y()); \ | 45 EXPECT_EQ(a.y(), b.y()); \ |
46 EXPECT_EQ(a.width(), b.width()); \ | 46 EXPECT_EQ(a.width(), b.width()); \ |
47 EXPECT_EQ(a.height(), b.height()); | 47 EXPECT_EQ(a.height(), b.height()); |
(...skipping 16 matching lines...) Expand all Loading... |
64 int alpha = *bitmap.getAddr32(x, y) >> 24; \ | 64 int alpha = *bitmap.getAddr32(x, y) >> 24; \ |
65 bool opaque = opaqueRect.contains(x, y); \ | 65 bool opaque = opaqueRect.contains(x, y); \ |
66 EXPECT_EQ(opaque, alpha == 255); \ | 66 EXPECT_EQ(opaque, alpha == 255); \ |
67 } \ | 67 } \ |
68 } | 68 } |
69 | 69 |
70 TEST(GraphicsContextTest, pictureRecording) { | 70 TEST(GraphicsContextTest, pictureRecording) { |
71 SkBitmap bitmap; | 71 SkBitmap bitmap; |
72 bitmap.allocN32Pixels(100, 100); | 72 bitmap.allocN32Pixels(100, 100); |
73 bitmap.eraseColor(0); | 73 bitmap.eraseColor(0); |
74 SkCanvas canvas(bitmap); | 74 SkCanvas bitmapCanvas(bitmap); |
| 75 PaintCanvasPassThrough canvas(&bitmapCanvas); |
75 | 76 |
76 std::unique_ptr<PaintController> paintController = PaintController::create(); | 77 std::unique_ptr<PaintController> paintController = PaintController::create(); |
77 GraphicsContext context(*paintController); | 78 GraphicsContext context(*paintController); |
78 | 79 |
79 Color opaque(1.0f, 0.0f, 0.0f, 1.0f); | 80 Color opaque(1.0f, 0.0f, 0.0f, 1.0f); |
80 FloatRect bounds(0, 0, 100, 100); | 81 FloatRect bounds(0, 0, 100, 100); |
81 | 82 |
82 context.beginRecording(bounds); | 83 context.beginRecording(bounds); |
83 context.fillRect(FloatRect(0, 0, 50, 50), opaque, SkBlendMode::kSrcOver); | 84 context.fillRect(FloatRect(0, 0, 50, 50), opaque, SkBlendMode::kSrcOver); |
84 sk_sp<const SkPicture> picture = context.endRecording(); | 85 sk_sp<const PaintRecord> picture = context.endRecording(); |
85 canvas.drawPicture(picture.get()); | 86 canvas.drawPicture(picture.get()); |
86 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(0, 0, 50, 50)) | 87 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(0, 0, 50, 50)) |
87 | 88 |
88 context.beginRecording(bounds); | 89 context.beginRecording(bounds); |
89 context.fillRect(FloatRect(0, 0, 100, 100), opaque, SkBlendMode::kSrcOver); | 90 context.fillRect(FloatRect(0, 0, 100, 100), opaque, SkBlendMode::kSrcOver); |
90 picture = context.endRecording(); | 91 picture = context.endRecording(); |
91 // Make sure the opaque region was unaffected by the rect drawn during Picture | 92 // Make sure the opaque region was unaffected by the rect drawn during Picture |
92 // recording. | 93 // recording. |
93 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(0, 0, 50, 50)) | 94 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(0, 0, 50, 50)) |
94 | 95 |
95 canvas.drawPicture(picture.get()); | 96 canvas.drawPicture(picture.get()); |
96 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(0, 0, 100, 100)) | 97 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(0, 0, 100, 100)) |
97 } | 98 } |
98 | 99 |
99 TEST(GraphicsContextTest, UnboundedDrawsAreClipped) { | 100 TEST(GraphicsContextTest, UnboundedDrawsAreClipped) { |
100 SkBitmap bitmap; | 101 SkBitmap bitmap; |
101 bitmap.allocN32Pixels(400, 400); | 102 bitmap.allocN32Pixels(400, 400); |
102 bitmap.eraseColor(0); | 103 bitmap.eraseColor(0); |
103 SkCanvas canvas(bitmap); | 104 SkCanvas bitmapCanvas(bitmap); |
| 105 PaintCanvasPassThrough canvas(&bitmapCanvas); |
104 | 106 |
105 Color opaque(1.0f, 0.0f, 0.0f, 1.0f); | 107 Color opaque(1.0f, 0.0f, 0.0f, 1.0f); |
106 Color alpha(0.0f, 0.0f, 0.0f, 0.0f); | 108 Color alpha(0.0f, 0.0f, 0.0f, 0.0f); |
107 FloatRect bounds(0, 0, 100, 100); | 109 FloatRect bounds(0, 0, 100, 100); |
108 | 110 |
109 std::unique_ptr<PaintController> paintController = PaintController::create(); | 111 std::unique_ptr<PaintController> paintController = PaintController::create(); |
110 GraphicsContext context(*paintController); | 112 GraphicsContext context(*paintController); |
111 context.beginRecording(bounds); | 113 context.beginRecording(bounds); |
112 | 114 |
113 context.setShouldAntialias(false); | 115 context.setShouldAntialias(false); |
114 context.setMiterLimit(1); | 116 context.setMiterLimit(1); |
115 context.setStrokeThickness(5); | 117 context.setStrokeThickness(5); |
116 context.setLineCap(SquareCap); | 118 context.setLineCap(SquareCap); |
117 context.setStrokeStyle(SolidStroke); | 119 context.setStrokeStyle(SolidStroke); |
118 | 120 |
119 // Make skia unable to compute fast bounds for our paths. | 121 // Make skia unable to compute fast bounds for our paths. |
120 DashArray dashArray; | 122 DashArray dashArray; |
121 dashArray.push_back(1); | 123 dashArray.push_back(1); |
122 dashArray.push_back(0); | 124 dashArray.push_back(0); |
123 context.setLineDash(dashArray, 0); | 125 context.setLineDash(dashArray, 0); |
124 | 126 |
125 // Make the device opaque in 10,10 40x40. | 127 // Make the device opaque in 10,10 40x40. |
126 context.fillRect(FloatRect(10, 10, 40, 40), opaque, SkBlendMode::kSrcOver); | 128 context.fillRect(FloatRect(10, 10, 40, 40), opaque, SkBlendMode::kSrcOver); |
127 sk_sp<const SkPicture> picture = context.endRecording(); | 129 sk_sp<const PaintRecord> picture = context.endRecording(); |
128 canvas.drawPicture(picture.get()); | 130 canvas.drawPicture(picture.get()); |
129 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(10, 10, 40, 40)); | 131 EXPECT_OPAQUE_PIXELS_ONLY_IN_RECT(bitmap, IntRect(10, 10, 40, 40)); |
130 | 132 |
131 context.beginRecording(bounds); | 133 context.beginRecording(bounds); |
132 // Clip to the left edge of the opaque area. | 134 // Clip to the left edge of the opaque area. |
133 context.clip(IntRect(10, 10, 10, 40)); | 135 context.clip(IntRect(10, 10, 10, 40)); |
134 | 136 |
135 // Draw a path that gets clipped. This should destroy the opaque area, but | 137 // Draw a path that gets clipped. This should destroy the opaque area, but |
136 // only inside the clip. | 138 // only inside the clip. |
137 Path path; | 139 Path path; |
138 path.moveTo(FloatPoint(10, 10)); | 140 path.moveTo(FloatPoint(10, 10)); |
139 path.addLineTo(FloatPoint(40, 40)); | 141 path.addLineTo(FloatPoint(40, 40)); |
140 SkPaint paint; | 142 PaintFlags paint; |
141 paint.setColor(alpha.rgb()); | 143 paint.setColor(alpha.rgb()); |
142 paint.setBlendMode(SkBlendMode::kSrcOut); | 144 paint.setBlendMode(SkBlendMode::kSrcOut); |
143 context.drawPath(path.getSkPath(), paint); | 145 context.drawPath(path.getSkPath(), paint); |
144 | 146 |
145 picture = context.endRecording(); | 147 picture = context.endRecording(); |
146 canvas.drawPicture(picture.get()); | 148 canvas.drawPicture(picture.get()); |
147 EXPECT_OPAQUE_PIXELS_IN_RECT(bitmap, IntRect(20, 10, 30, 40)); | 149 EXPECT_OPAQUE_PIXELS_IN_RECT(bitmap, IntRect(20, 10, 30, 40)); |
148 } | 150 } |
149 | 151 |
150 } // namespace blink | 152 } // namespace blink |
OLD | NEW |