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