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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 { \ | 59 { \ |
60 SkAutoLockPixels locker(bitmap); \ | 60 SkAutoLockPixels locker(bitmap); \ |
61 for (int y = 0; y < bitmap.height(); ++y) \ | 61 for (int y = 0; y < bitmap.height(); ++y) \ |
62 for (int x = 0; x < bitmap.width(); ++x) { \ | 62 for (int x = 0; x < bitmap.width(); ++x) { \ |
63 int alpha = *bitmap.getAddr32(x, y) >> 24; \ | 63 int alpha = *bitmap.getAddr32(x, y) >> 24; \ |
64 bool opaque = opaqueRect.contains(x, y); \ | 64 bool opaque = opaqueRect.contains(x, y); \ |
65 EXPECT_EQ(opaque, alpha == 255); \ | 65 EXPECT_EQ(opaque, alpha == 255); \ |
66 } \ | 66 } \ |
67 } | 67 } |
68 | 68 |
69 #define EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, color) \ | |
70 { \ | |
71 SkAutoLockPixels locker(bitmap); \ | |
72 for (int y = rect.y(); y < rect.maxY(); ++y) { \ | |
73 for (int x = rect.x(); x < rect.maxX(); ++x) { \ | |
74 RGBA32 pixel = *bitmap.getAddr32(x, y); \ | |
75 EXPECT_EQ(color, pixel); \ | |
76 } \ | |
77 } \ | |
78 } | |
79 | |
69 TEST(GraphicsContextTest, trackOpaqueTest) | 80 TEST(GraphicsContextTest, trackOpaqueTest) |
70 { | 81 { |
71 SkBitmap bitmap; | 82 SkBitmap bitmap; |
72 bitmap.allocN32Pixels(400, 400); | 83 bitmap.allocN32Pixels(400, 400); |
73 bitmap.eraseColor(0); | 84 bitmap.eraseColor(0); |
74 SkCanvas canvas(bitmap); | 85 SkCanvas canvas(bitmap); |
75 | 86 |
76 GraphicsContext context(&canvas); | 87 GraphicsContext context(&canvas); |
77 context.setRegionTrackingMode(GraphicsContext::RegionTrackingOpaque); | 88 context.setRegionTrackingMode(GraphicsContext::RegionTrackingOpaque); |
78 | 89 |
(...skipping 1128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1207 | 1218 |
1208 // endRecording finally makes the picture accessible | 1219 // endRecording finally makes the picture accessible |
1209 RefPtr<DisplayList> dl = context.endRecording(); | 1220 RefPtr<DisplayList> dl = context.endRecording(); |
1210 SkPicture* pic = dl->picture(); | 1221 SkPicture* pic = dl->picture(); |
1211 EXPECT_TRUE(pic); | 1222 EXPECT_TRUE(pic); |
1212 EXPECT_EQ(1, pic->getRefCnt()); | 1223 EXPECT_EQ(1, pic->getRefCnt()); |
1213 | 1224 |
1214 context.endRecording(); | 1225 context.endRecording(); |
1215 } | 1226 } |
1216 | 1227 |
1228 TEST(GraphicsContextTest, BeginLayerPreserveCanvasState) | |
1229 { | |
1230 SkBitmap bitmap; | |
1231 bitmap.allocN32Pixels(1, 1); | |
1232 bitmap.eraseColor(0); | |
1233 SkCanvas canvas(bitmap); | |
1234 GraphicsContext context(&canvas); | |
1235 | |
1236 IntRect rect(0, 0, 1, 1); | |
1237 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::transparent); | |
1238 context.fillRect(rect, Color::gray); | |
1239 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); | |
1240 | |
1241 // Translate out of bound not to draw. | |
1242 context.translate(1, 0); | |
1243 context.fillRect(rect, Color::darkGray); | |
1244 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); | |
1245 EXPECT_EQ(SK_Scalar1, context.getTotalMatrix().getTranslateX()); | |
1246 | |
1247 context.beginLayer(1, CompositeSourceOver); | |
1248 context.translate(-1, 0); | |
1249 EXPECT_EQ(0, context.getTotalMatrix().getTranslateX()); | |
1250 context.fillRect(rect, Color::darkGray); | |
1251 context.endLayer(); | |
1252 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); | |
1253 | |
1254 // GraphicsContext::beginLayer()/endLayer() behaves the same as save()/resto re() | |
1255 // so current transform is restored. | |
1256 EXPECT_EQ(SK_Scalar1, context.getTotalMatrix().getTranslateX()); | |
1257 | |
1258 context.fillRect(rect, Color::lightGray); | |
1259 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); | |
1260 } | |
1261 | |
1262 // Although this test looks like duplicated to BeginLayerPreserveCanvasState, | |
1263 // this is needed because GraphicsContext keeps a composite operator in SkPaint | |
1264 // while setting a transform to SkCanvas directly. | |
1265 TEST(GraphicsContextTest, BeginLayerPreservePaintState) | |
1266 { | |
1267 SkBitmap bitmap; | |
1268 bitmap.allocN32Pixels(1, 1); | |
1269 bitmap.eraseColor(0); | |
1270 SkCanvas canvas(bitmap); | |
1271 GraphicsContext context(&canvas); | |
1272 | |
1273 IntRect rect(0, 0, 1, 1); | |
1274 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::transparent); | |
1275 EXPECT_EQ(CompositeSourceOver, context.compositeOperation()); | |
1276 context.fillRect(rect, Color::gray); | |
1277 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); | |
1278 | |
1279 // Set CompositeDestinationIn not to draw. | |
1280 context.setCompositeOperation(CompositeDestinationIn); | |
1281 context.fillRect(rect, Color::darkGray); | |
1282 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::gray); | |
1283 EXPECT_EQ(CompositeDestinationIn, context.compositeOperation()); | |
1284 | |
1285 context.beginLayer(1, CompositeSourceIn); | |
1286 EXPECT_EQ(CompositeDestinationIn, context.compositeOperation()); | |
1287 context.setCompositeOperation(CompositeSourceOver); | |
1288 EXPECT_EQ(CompositeSourceOver, context.compositeOperation()); | |
1289 context.fillRect(rect, Color::darkGray); | |
1290 context.endLayer(); | |
1291 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); | |
1292 | |
1293 // GraphicsContext::beginLayer()/endLayer() behaves the same as save()/resto re() | |
1294 // so current composite operator is restored. | |
1295 EXPECT_EQ(CompositeDestinationIn, context.compositeOperation()); | |
dshwang
2014/10/14 17:25:07
Without this CL, this line and #1298 fail, while B
| |
1296 | |
1297 context.fillRect(rect, Color::lightGray); | |
1298 EXPECT_PIXELS_MATCH_COLOR(bitmap, rect, Color::darkGray); | |
1299 } | |
1300 | |
1217 } // namespace | 1301 } // namespace |
OLD | NEW |