OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/compiler_specific.h" | |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "cc/test/geometry_test_utils.h" | |
8 #include "skia/ext/lazy_pixel_ref.h" | |
9 #include "skia/ext/lazy_pixel_ref_utils.h" | |
10 #include "skia/ext/refptr.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 #include "third_party/skia/include/core/SkBitmap.h" | |
13 #include "third_party/skia/include/core/SkCanvas.h" | |
14 #include "third_party/skia/include/core/SkFlattenableBuffers.h" | |
15 #include "third_party/skia/include/core/SkPoint.h" | |
16 #include "third_party/skia/include/core/SkShader.h" | |
17 #include "third_party/skia/src/core/SkOrderedReadBuffer.h" | |
18 #include "ui/gfx/rect.h" | |
19 #include "ui/gfx/skia_util.h" | |
20 | |
21 namespace skia { | |
22 | |
23 namespace { | |
24 | |
25 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap); | |
26 | |
27 class TestLazyPixelRef : public skia::LazyPixelRef { | |
28 public: | |
29 TestLazyPixelRef(const SkImageInfo& info); | |
30 virtual ~TestLazyPixelRef(); | |
31 | |
32 virtual SkFlattenable::Factory getFactory() const OVERRIDE; | |
33 virtual void* onLockPixels(SkColorTable** color_table) OVERRIDE; | |
34 virtual void onUnlockPixels() OVERRIDE {} | |
35 virtual bool PrepareToDecode(const PrepareParams& params) OVERRIDE; | |
36 virtual bool MaybeDecoded() OVERRIDE; | |
37 virtual SkPixelRef* deepCopy(SkBitmap::Config config, const SkIRect* subset) | |
38 OVERRIDE; | |
39 virtual void Decode() OVERRIDE {} | |
40 | |
41 private: | |
42 scoped_ptr<char[]> pixels_; | |
43 }; | |
44 | |
45 class TestLazyShader : public SkShader { | |
46 public: | |
47 TestLazyShader() { CreateBitmap(gfx::Size(50, 50), "lazy", &bitmap_); } | |
48 | |
49 TestLazyShader(SkFlattenableReadBuffer& flattenable_buffer) { | |
50 SkOrderedReadBuffer& buffer = | |
51 static_cast<SkOrderedReadBuffer&>(flattenable_buffer); | |
52 SkReader32* reader = buffer.getReader32(); | |
53 | |
54 reader->skip(-4); | |
55 uint32_t toSkip = reader->readU32(); | |
56 reader->skip(toSkip); | |
57 | |
58 CreateBitmap(gfx::Size(50, 50), "lazy", &bitmap_); | |
59 } | |
60 | |
61 virtual SkShader::BitmapType asABitmap(SkBitmap* bitmap, | |
62 SkMatrix* matrix, | |
63 TileMode xy[2]) const OVERRIDE { | |
64 if (bitmap) | |
65 *bitmap = bitmap_; | |
66 return SkShader::kDefault_BitmapType; | |
67 } | |
68 | |
69 // Pure virtual implementaiton. | |
70 virtual void shadeSpan(int x, int y, SkPMColor[], int count) OVERRIDE {} | |
71 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLazyShader); | |
72 | |
73 private: | |
74 SkBitmap bitmap_; | |
75 }; | |
76 | |
77 TestLazyPixelRef::TestLazyPixelRef(const SkImageInfo& info) | |
78 : skia::LazyPixelRef(info), | |
79 pixels_(new char[4 * info.fWidth * info.fHeight]) {} | |
80 | |
81 TestLazyPixelRef::~TestLazyPixelRef() {} | |
82 | |
83 SkFlattenable::Factory TestLazyPixelRef::getFactory() const { return NULL; } | |
84 | |
85 void* TestLazyPixelRef::onLockPixels(SkColorTable** color_table) { | |
86 return pixels_.get(); | |
87 } | |
88 | |
89 bool TestLazyPixelRef::PrepareToDecode(const PrepareParams& params) { | |
90 return true; | |
91 } | |
92 | |
93 bool TestLazyPixelRef::MaybeDecoded() { | |
94 return true; | |
95 } | |
96 | |
97 SkPixelRef* TestLazyPixelRef::deepCopy(SkBitmap::Config config, | |
98 const SkIRect* subset) { | |
99 this->ref(); | |
100 return this; | |
101 } | |
102 | |
103 void CreateBitmap(gfx::Size size, const char* uri, SkBitmap* bitmap) { | |
104 const SkImageInfo info = { | |
105 size.width(), size.height(), kPMColor_SkColorType, kPremul_SkAlphaType | |
106 }; | |
107 | |
108 skia::RefPtr<TestLazyPixelRef> lazy_pixel_ref = | |
109 skia::AdoptRef(new TestLazyPixelRef(info)); | |
110 lazy_pixel_ref->setURI(uri); | |
111 | |
112 bitmap->setConfig(info); | |
113 bitmap->setPixelRef(lazy_pixel_ref.get()); | |
114 } | |
115 | |
116 SkCanvas* StartRecording(SkPicture* picture, gfx::Rect layer_rect) { | |
117 SkCanvas* canvas = picture->beginRecording( | |
118 layer_rect.width(), | |
119 layer_rect.height(), | |
120 SkPicture::kUsePathBoundsForClip_RecordingFlag | | |
121 SkPicture::kOptimizeForClippedPlayback_RecordingFlag); | |
122 | |
123 canvas->save(); | |
124 canvas->translate(-layer_rect.x(), -layer_rect.y()); | |
125 canvas->clipRect(SkRect::MakeXYWH( | |
126 layer_rect.x(), layer_rect.y(), layer_rect.width(), layer_rect.height())); | |
127 | |
128 return canvas; | |
129 } | |
130 | |
131 void StopRecording(SkPicture* picture, SkCanvas* canvas) { | |
132 canvas->restore(); | |
133 picture->endRecording(); | |
134 } | |
135 | |
136 } // namespace | |
137 | |
138 TEST(LazyPixelRefUtilsTest, DrawPaint) { | |
139 gfx::Rect layer_rect(0, 0, 256, 256); | |
140 | |
141 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
142 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
143 | |
144 TestLazyShader first_shader; | |
145 SkPaint first_paint; | |
146 first_paint.setShader(&first_shader); | |
147 | |
148 TestLazyShader second_shader; | |
149 SkPaint second_paint; | |
150 second_paint.setShader(&second_shader); | |
151 | |
152 TestLazyShader third_shader; | |
153 SkPaint third_paint; | |
154 third_paint.setShader(&third_shader); | |
155 | |
156 canvas->drawPaint(first_paint); | |
157 canvas->clipRect(SkRect::MakeXYWH(34, 45, 56, 67)); | |
158 canvas->drawPaint(second_paint); | |
159 // Total clip is now (34, 45, 56, 55) | |
160 canvas->clipRect(SkRect::MakeWH(100, 100)); | |
161 canvas->drawPaint(third_paint); | |
162 | |
163 StopRecording(picture.get(), canvas); | |
164 | |
165 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
166 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
167 | |
168 EXPECT_EQ(3u, pixel_refs.size()); | |
169 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 256, 256), | |
170 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
171 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 67), | |
172 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
173 EXPECT_FLOAT_RECT_EQ(gfx::RectF(34, 45, 56, 55), | |
174 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
175 } | |
176 | |
177 TEST(LazyPixelRefUtilsTest, DrawPoints) { | |
178 gfx::Rect layer_rect(0, 0, 256, 256); | |
179 | |
180 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
181 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
182 | |
183 TestLazyShader first_shader; | |
184 SkPaint first_paint; | |
185 first_paint.setShader(&first_shader); | |
186 | |
187 TestLazyShader second_shader; | |
188 SkPaint second_paint; | |
189 second_paint.setShader(&second_shader); | |
190 | |
191 TestLazyShader third_shader; | |
192 SkPaint third_paint; | |
193 third_paint.setShader(&third_shader); | |
194 | |
195 SkPoint points[3]; | |
196 points[0].set(10, 10); | |
197 points[1].set(100, 20); | |
198 points[2].set(50, 100); | |
199 // (10, 10, 90, 90). | |
200 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, first_paint); | |
201 | |
202 canvas->save(); | |
203 | |
204 canvas->clipRect(SkRect::MakeWH(50, 50)); | |
205 // (10, 10, 40, 40). | |
206 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, second_paint); | |
207 | |
208 canvas->restore(); | |
209 | |
210 points[0].set(50, 55); | |
211 points[1].set(50, 55); | |
212 points[2].set(200, 200); | |
213 // (50, 55, 150, 145). | |
214 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, third_paint); | |
215 | |
216 StopRecording(picture.get(), canvas); | |
217 | |
218 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
219 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
220 | |
221 EXPECT_EQ(3u, pixel_refs.size()); | |
222 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90), | |
223 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
224 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40), | |
225 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
226 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145), | |
227 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
228 } | |
229 | |
230 TEST(LazyPixelRefUtilsTest, DrawRect) { | |
231 gfx::Rect layer_rect(0, 0, 256, 256); | |
232 | |
233 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
234 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
235 | |
236 TestLazyShader first_shader; | |
237 SkPaint first_paint; | |
238 first_paint.setShader(&first_shader); | |
239 | |
240 TestLazyShader second_shader; | |
241 SkPaint second_paint; | |
242 second_paint.setShader(&second_shader); | |
243 | |
244 TestLazyShader third_shader; | |
245 SkPaint third_paint; | |
246 third_paint.setShader(&third_shader); | |
247 | |
248 // (10, 20, 30, 40). | |
249 canvas->drawRect(SkRect::MakeXYWH(10, 20, 30, 40), first_paint); | |
250 | |
251 canvas->save(); | |
252 | |
253 canvas->translate(5, 17); | |
254 // (5, 50, 25, 35) | |
255 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint); | |
256 | |
257 canvas->restore(); | |
258 | |
259 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50)); | |
260 canvas->translate(20, 20); | |
261 // (50, 50, 50, 50) | |
262 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint); | |
263 | |
264 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
265 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
266 | |
267 EXPECT_EQ(3u, pixel_refs.size()); | |
268 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40), | |
269 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
270 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35), | |
271 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
272 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), | |
273 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
274 } | |
275 | |
276 TEST(LazyPixelRefUtilsTest, DrawRRect) { | |
277 gfx::Rect layer_rect(0, 0, 256, 256); | |
278 | |
279 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
280 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
281 | |
282 TestLazyShader first_shader; | |
283 SkPaint first_paint; | |
284 first_paint.setShader(&first_shader); | |
285 | |
286 TestLazyShader second_shader; | |
287 SkPaint second_paint; | |
288 second_paint.setShader(&second_shader); | |
289 | |
290 TestLazyShader third_shader; | |
291 SkPaint third_paint; | |
292 third_paint.setShader(&third_shader); | |
293 | |
294 SkRRect rrect; | |
295 rrect.setRect(SkRect::MakeXYWH(10, 20, 30, 40)); | |
296 | |
297 // (10, 20, 30, 40). | |
298 canvas->drawRRect(rrect, first_paint); | |
299 | |
300 canvas->save(); | |
301 | |
302 canvas->translate(5, 17); | |
303 rrect.setRect(SkRect::MakeXYWH(0, 33, 25, 35)); | |
304 // (5, 50, 25, 35) | |
305 canvas->drawRRect(rrect, second_paint); | |
306 | |
307 canvas->restore(); | |
308 | |
309 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50)); | |
310 canvas->translate(20, 20); | |
311 rrect.setRect(SkRect::MakeXYWH(0, 0, 100, 100)); | |
312 // (50, 50, 50, 50) | |
313 canvas->drawRRect(rrect, third_paint); | |
314 | |
315 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
316 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
317 | |
318 EXPECT_EQ(3u, pixel_refs.size()); | |
319 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 20, 30, 40), | |
320 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
321 EXPECT_FLOAT_RECT_EQ(gfx::RectF(5, 50, 25, 35), | |
322 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
323 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), | |
324 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
325 } | |
326 | |
327 TEST(LazyPixelRefUtilsTest, DrawOval) { | |
328 gfx::Rect layer_rect(0, 0, 256, 256); | |
329 | |
330 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
331 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
332 | |
333 TestLazyShader first_shader; | |
334 SkPaint first_paint; | |
335 first_paint.setShader(&first_shader); | |
336 | |
337 TestLazyShader second_shader; | |
338 SkPaint second_paint; | |
339 second_paint.setShader(&second_shader); | |
340 | |
341 TestLazyShader third_shader; | |
342 SkPaint third_paint; | |
343 third_paint.setShader(&third_shader); | |
344 | |
345 canvas->save(); | |
346 | |
347 canvas->scale(2, 0.5); | |
348 // (20, 10, 60, 20). | |
349 canvas->drawOval(SkRect::MakeXYWH(10, 20, 30, 40), first_paint); | |
350 | |
351 canvas->restore(); | |
352 canvas->save(); | |
353 | |
354 canvas->translate(1, 2); | |
355 // (1, 35, 25, 35) | |
356 canvas->drawRect(SkRect::MakeXYWH(0, 33, 25, 35), second_paint); | |
357 | |
358 canvas->restore(); | |
359 | |
360 canvas->clipRect(SkRect::MakeXYWH(50, 50, 50, 50)); | |
361 canvas->translate(20, 20); | |
362 // (50, 50, 50, 50) | |
363 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), third_paint); | |
364 | |
365 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
366 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
367 | |
368 EXPECT_EQ(3u, pixel_refs.size()); | |
369 EXPECT_FLOAT_RECT_EQ(gfx::RectF(20, 10, 60, 20), | |
370 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
371 EXPECT_FLOAT_RECT_EQ(gfx::RectF(1, 35, 25, 35), | |
372 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
373 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), | |
374 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
375 } | |
376 | |
377 TEST(LazyPixelRefUtilsTest, DrawPath) { | |
378 gfx::Rect layer_rect(0, 0, 256, 256); | |
379 | |
380 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
381 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
382 | |
383 TestLazyShader first_shader; | |
384 SkPaint first_paint; | |
385 first_paint.setShader(&first_shader); | |
386 | |
387 TestLazyShader second_shader; | |
388 SkPaint second_paint; | |
389 second_paint.setShader(&second_shader); | |
390 | |
391 SkPath path; | |
392 path.moveTo(12, 13); | |
393 path.lineTo(50, 50); | |
394 path.lineTo(22, 101); | |
395 | |
396 // (12, 13, 38, 88). | |
397 canvas->drawPath(path, first_paint); | |
398 | |
399 canvas->save(); | |
400 canvas->clipRect(SkRect::MakeWH(50, 50)); | |
401 | |
402 // (12, 13, 38, 37). | |
403 canvas->drawPath(path, second_paint); | |
404 | |
405 canvas->restore(); | |
406 | |
407 StopRecording(picture.get(), canvas); | |
408 | |
409 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
410 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
411 | |
412 EXPECT_EQ(2u, pixel_refs.size()); | |
413 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 88), | |
414 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
415 EXPECT_FLOAT_RECT_EQ(gfx::RectF(12, 13, 38, 37), | |
416 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
417 } | |
418 | |
419 TEST(LazyPixelRefUtilsTest, DrawBitmap) { | |
420 gfx::Rect layer_rect(0, 0, 256, 256); | |
421 | |
422 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
423 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
424 | |
425 SkBitmap first; | |
426 CreateBitmap(gfx::Size(50, 50), "lazy", &first); | |
427 SkBitmap second; | |
428 CreateBitmap(gfx::Size(50, 50), "lazy", &second); | |
429 SkBitmap third; | |
430 CreateBitmap(gfx::Size(50, 50), "lazy", &third); | |
431 SkBitmap fourth; | |
432 CreateBitmap(gfx::Size(50, 1), "lazy", &fourth); | |
433 SkBitmap fifth; | |
434 CreateBitmap(gfx::Size(10, 10), "lazy", &fifth); | |
435 | |
436 canvas->save(); | |
437 | |
438 // At (0, 0). | |
439 canvas->drawBitmap(first, 0, 0); | |
440 canvas->translate(25, 0); | |
441 // At (25, 0). | |
442 canvas->drawBitmap(second, 0, 0); | |
443 canvas->translate(0, 50); | |
444 // At (50, 50). | |
445 canvas->drawBitmap(third, 25, 0); | |
446 | |
447 canvas->restore(); | |
448 canvas->save(); | |
449 | |
450 canvas->translate(1, 0); | |
451 canvas->rotate(90); | |
452 // At (1, 0), rotated 90 degrees | |
453 canvas->drawBitmap(fourth, 0, 0); | |
454 | |
455 canvas->restore(); | |
456 | |
457 canvas->scale(5, 6); | |
458 // At (0, 0), scaled by 5 and 6 | |
459 canvas->drawBitmap(fifth, 0, 0); | |
460 | |
461 StopRecording(picture.get(), canvas); | |
462 | |
463 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
464 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
465 | |
466 EXPECT_EQ(5u, pixel_refs.size()); | |
467 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50), | |
468 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
469 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50), | |
470 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
471 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50), | |
472 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
473 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50), | |
474 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); | |
475 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60), | |
476 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect)); | |
477 | |
478 } | |
479 | |
480 TEST(LazyPixelRefUtilsTest, DrawBitmapRect) { | |
481 gfx::Rect layer_rect(0, 0, 256, 256); | |
482 | |
483 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
484 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
485 | |
486 SkBitmap first; | |
487 CreateBitmap(gfx::Size(50, 50), "lazy", &first); | |
488 SkBitmap second; | |
489 CreateBitmap(gfx::Size(50, 50), "lazy", &second); | |
490 SkBitmap third; | |
491 CreateBitmap(gfx::Size(50, 50), "lazy", &third); | |
492 | |
493 TestLazyShader first_shader; | |
494 SkPaint first_paint; | |
495 first_paint.setShader(&first_shader); | |
496 | |
497 SkPaint non_lazy_paint; | |
498 | |
499 canvas->save(); | |
500 | |
501 // (0, 0, 100, 100). | |
502 canvas->drawBitmapRect(first, SkRect::MakeWH(100, 100), &non_lazy_paint); | |
503 canvas->translate(25, 0); | |
504 // (75, 50, 10, 10). | |
505 canvas->drawBitmapRect( | |
506 second, SkRect::MakeXYWH(50, 50, 10, 10), &non_lazy_paint); | |
507 canvas->translate(5, 50); | |
508 // (0, 30, 100, 100). One from bitmap, one from paint. | |
509 canvas->drawBitmapRect( | |
510 third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint); | |
511 | |
512 canvas->restore(); | |
513 | |
514 StopRecording(picture.get(), canvas); | |
515 | |
516 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
517 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
518 | |
519 EXPECT_EQ(4u, pixel_refs.size()); | |
520 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100), | |
521 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
522 EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10), | |
523 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
524 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), | |
525 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
526 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100), | |
527 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); | |
528 } | |
529 | |
530 TEST(LazyPixelRefUtilsTest, DrawSprite) { | |
531 gfx::Rect layer_rect(0, 0, 256, 256); | |
532 | |
533 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
534 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
535 | |
536 SkBitmap first; | |
537 CreateBitmap(gfx::Size(50, 50), "lazy", &first); | |
538 SkBitmap second; | |
539 CreateBitmap(gfx::Size(50, 50), "lazy", &second); | |
540 SkBitmap third; | |
541 CreateBitmap(gfx::Size(50, 50), "lazy", &third); | |
542 SkBitmap fourth; | |
543 CreateBitmap(gfx::Size(50, 50), "lazy", &fourth); | |
544 SkBitmap fifth; | |
545 CreateBitmap(gfx::Size(50, 50), "lazy", &fifth); | |
546 | |
547 canvas->save(); | |
548 | |
549 // Sprites aren't affected by the current matrix. | |
550 | |
551 // (0, 0, 50, 50). | |
552 canvas->drawSprite(first, 0, 0); | |
553 canvas->translate(25, 0); | |
554 // (10, 0, 50, 50). | |
555 canvas->drawSprite(second, 10, 0); | |
556 canvas->translate(0, 50); | |
557 // (25, 0, 50, 50). | |
558 canvas->drawSprite(third, 25, 0); | |
559 | |
560 canvas->restore(); | |
561 canvas->save(); | |
562 | |
563 canvas->rotate(90); | |
564 // (0, 0, 50, 50). | |
565 canvas->drawSprite(fourth, 0, 0); | |
566 | |
567 canvas->restore(); | |
568 | |
569 TestLazyShader first_shader; | |
570 SkPaint first_paint; | |
571 first_paint.setShader(&first_shader); | |
572 | |
573 canvas->scale(5, 6); | |
574 // (100, 100, 50, 50). | |
575 canvas->drawSprite(fifth, 100, 100, &first_paint); | |
576 | |
577 StopRecording(picture.get(), canvas); | |
578 | |
579 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
580 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
581 | |
582 EXPECT_EQ(6u, pixel_refs.size()); | |
583 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50), | |
584 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
585 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50), | |
586 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
587 EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50), | |
588 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
589 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50), | |
590 gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect)); | |
591 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50), | |
592 gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect)); | |
593 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50), | |
594 gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect)); | |
595 } | |
596 | |
597 TEST(LazyPixelRefUtilsTest, DrawText) { | |
598 gfx::Rect layer_rect(0, 0, 256, 256); | |
599 | |
600 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
601 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
602 | |
603 TestLazyShader first_shader; | |
604 SkPaint first_paint; | |
605 first_paint.setShader(&first_shader); | |
606 | |
607 SkPoint points[4]; | |
608 points[0].set(10, 50); | |
609 points[1].set(20, 50); | |
610 points[2].set(30, 50); | |
611 points[3].set(40, 50); | |
612 | |
613 SkPath path; | |
614 path.moveTo(10, 50); | |
615 path.lineTo(20, 50); | |
616 path.lineTo(30, 50); | |
617 path.lineTo(40, 50); | |
618 path.lineTo(50, 50); | |
619 | |
620 canvas->drawText("text", 4, 50, 50, first_paint); | |
621 canvas->drawPosText("text", 4, points, first_paint); | |
622 canvas->drawTextOnPath("text", 4, path, NULL, first_paint); | |
623 | |
624 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
625 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
626 | |
627 EXPECT_EQ(3u, pixel_refs.size()); | |
628 } | |
629 | |
630 TEST(LazyPixelRefUtilsTest, DrawVertices) { | |
631 gfx::Rect layer_rect(0, 0, 256, 256); | |
632 | |
633 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
634 SkCanvas* canvas = StartRecording(picture.get(), layer_rect); | |
635 | |
636 TestLazyShader first_shader; | |
637 SkPaint first_paint; | |
638 first_paint.setShader(&first_shader); | |
639 | |
640 TestLazyShader second_shader; | |
641 SkPaint second_paint; | |
642 second_paint.setShader(&second_shader); | |
643 | |
644 TestLazyShader third_shader; | |
645 SkPaint third_paint; | |
646 third_paint.setShader(&third_shader); | |
647 | |
648 SkPoint points[3]; | |
649 SkColor colors[3]; | |
650 uint16_t indecies[3] = {0, 1, 2}; | |
651 points[0].set(10, 10); | |
652 points[1].set(100, 20); | |
653 points[2].set(50, 100); | |
654 // (10, 10, 90, 90). | |
655 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, | |
656 3, | |
657 points, | |
658 points, | |
659 colors, | |
660 NULL, | |
661 indecies, | |
662 3, | |
663 first_paint); | |
664 | |
665 canvas->save(); | |
666 | |
667 canvas->clipRect(SkRect::MakeWH(50, 50)); | |
668 // (10, 10, 40, 40). | |
669 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, | |
670 3, | |
671 points, | |
672 points, | |
673 colors, | |
674 NULL, | |
675 indecies, | |
676 3, | |
677 second_paint); | |
678 | |
679 canvas->restore(); | |
680 | |
681 points[0].set(50, 55); | |
682 points[1].set(50, 55); | |
683 points[2].set(200, 200); | |
684 // (50, 55, 150, 145). | |
685 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, | |
686 3, | |
687 points, | |
688 points, | |
689 colors, | |
690 NULL, | |
691 indecies, | |
692 3, | |
693 third_paint); | |
694 | |
695 StopRecording(picture.get(), canvas); | |
696 | |
697 std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs; | |
698 skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs); | |
699 | |
700 EXPECT_EQ(3u, pixel_refs.size()); | |
701 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90), | |
702 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect)); | |
703 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40), | |
704 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect)); | |
705 EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145), | |
706 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect)); | |
707 } | |
708 | |
709 } // namespace skia | |
OLD | NEW |