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() const 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() const 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() const { 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() const { 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->translate(1, 0);
478  canvas->rotate(90);
479  // At (1, 0), rotated 90 degrees
480  canvas->drawBitmap(fourth, 0, 0);
481
482  canvas->restore();
483
484  canvas->scale(5, 6);
485  // At (0, 0), scaled by 5 and 6
486  canvas->drawBitmap(fifth, 0, 0);
487
488  StopRecording(picture.get(), canvas);
489
490  std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs;
491  skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs);
492
493  EXPECT_EQ(5u, pixel_refs.size());
494  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
495                       gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
496  EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
497                       gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
498  EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 50, 50, 50),
499                       gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
500  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 1, 50),
501                       gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
502  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 60),
503                       gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
504
505}
506
507TEST(LazyPixelRefUtilsTest, DrawBitmapRect) {
508  gfx::Rect layer_rect(0, 0, 256, 256);
509
510  skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
511  SkCanvas* canvas = StartRecording(picture.get(), layer_rect);
512
513  SkBitmap first;
514  CreateBitmap(gfx::Size(50, 50), "lazy", &first);
515  SkBitmap second;
516  CreateBitmap(gfx::Size(50, 50), "lazy", &second);
517  SkBitmap third;
518  CreateBitmap(gfx::Size(50, 50), "lazy", &third);
519
520  TestLazyShader first_shader;
521  SkPaint first_paint;
522  first_paint.setShader(&first_shader);
523
524  SkPaint non_lazy_paint;
525
526  canvas->save();
527
528  // (0, 0, 100, 100).
529  canvas->drawBitmapRect(first, SkRect::MakeWH(100, 100), &non_lazy_paint);
530  canvas->translate(25, 0);
531  // (75, 50, 10, 10).
532  canvas->drawBitmapRect(
533      second, SkRect::MakeXYWH(50, 50, 10, 10), &non_lazy_paint);
534  canvas->translate(5, 50);
535  // (0, 30, 100, 100). One from bitmap, one from paint.
536  canvas->drawBitmapRect(
537      third, SkRect::MakeXYWH(-30, -20, 100, 100), &first_paint);
538
539  canvas->restore();
540
541  StopRecording(picture.get(), canvas);
542
543  std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs;
544  skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs);
545
546  EXPECT_EQ(4u, pixel_refs.size());
547  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 100, 100),
548                       gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
549  EXPECT_FLOAT_RECT_EQ(gfx::RectF(75, 50, 10, 10),
550                       gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
551  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
552                       gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
553  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 30, 100, 100),
554                       gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
555}
556
557TEST(LazyPixelRefUtilsTest, DrawSprite) {
558  gfx::Rect layer_rect(0, 0, 256, 256);
559
560  skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
561  SkCanvas* canvas = StartRecording(picture.get(), layer_rect);
562
563  SkBitmap first;
564  CreateBitmap(gfx::Size(50, 50), "lazy", &first);
565  SkBitmap second;
566  CreateBitmap(gfx::Size(50, 50), "lazy", &second);
567  SkBitmap third;
568  CreateBitmap(gfx::Size(50, 50), "lazy", &third);
569  SkBitmap fourth;
570  CreateBitmap(gfx::Size(50, 50), "lazy", &fourth);
571  SkBitmap fifth;
572  CreateBitmap(gfx::Size(50, 50), "lazy", &fifth);
573
574  canvas->save();
575
576  // Sprites aren't affected by the current matrix.
577
578  // (0, 0, 50, 50).
579  canvas->drawSprite(first, 0, 0);
580  canvas->translate(25, 0);
581  // (10, 0, 50, 50).
582  canvas->drawSprite(second, 10, 0);
583  canvas->translate(0, 50);
584  // (25, 0, 50, 50).
585  canvas->drawSprite(third, 25, 0);
586
587  canvas->restore();
588  canvas->save();
589
590  canvas->rotate(90);
591  // (0, 0, 50, 50).
592  canvas->drawSprite(fourth, 0, 0);
593
594  canvas->restore();
595
596  TestLazyShader first_shader;
597  SkPaint first_paint;
598  first_paint.setShader(&first_shader);
599
600  canvas->scale(5, 6);
601  // (100, 100, 50, 50).
602  canvas->drawSprite(fifth, 100, 100, &first_paint);
603
604  StopRecording(picture.get(), canvas);
605
606  std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs;
607  skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs);
608
609  EXPECT_EQ(6u, pixel_refs.size());
610  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
611                       gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
612  EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 0, 50, 50),
613                       gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
614  EXPECT_FLOAT_RECT_EQ(gfx::RectF(25, 0, 50, 50),
615                       gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
616  EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 50, 50),
617                       gfx::SkRectToRectF(pixel_refs[3].pixel_ref_rect));
618  EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
619                       gfx::SkRectToRectF(pixel_refs[4].pixel_ref_rect));
620  EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 50, 50),
621                       gfx::SkRectToRectF(pixel_refs[5].pixel_ref_rect));
622}
623
624TEST(LazyPixelRefUtilsTest, DrawText) {
625  gfx::Rect layer_rect(0, 0, 256, 256);
626
627  skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
628  SkCanvas* canvas = StartRecording(picture.get(), layer_rect);
629
630  TestLazyShader first_shader;
631  SkPaint first_paint;
632  first_paint.setShader(&first_shader);
633
634  SkPoint points[4];
635  points[0].set(10, 50);
636  points[1].set(20, 50);
637  points[2].set(30, 50);
638  points[3].set(40, 50);
639
640  SkPath path;
641  path.moveTo(10, 50);
642  path.lineTo(20, 50);
643  path.lineTo(30, 50);
644  path.lineTo(40, 50);
645  path.lineTo(50, 50);
646
647  canvas->drawText("text", 4, 50, 50, first_paint);
648  canvas->drawPosText("text", 4, points, first_paint);
649  canvas->drawTextOnPath("text", 4, path, NULL, first_paint);
650
651  std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs;
652  skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs);
653
654  EXPECT_EQ(3u, pixel_refs.size());
655}
656
657TEST(LazyPixelRefUtilsTest, DrawVertices) {
658  gfx::Rect layer_rect(0, 0, 256, 256);
659
660  skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
661  SkCanvas* canvas = StartRecording(picture.get(), layer_rect);
662
663  TestLazyShader first_shader;
664  SkPaint first_paint;
665  first_paint.setShader(&first_shader);
666
667  TestLazyShader second_shader;
668  SkPaint second_paint;
669  second_paint.setShader(&second_shader);
670
671  TestLazyShader third_shader;
672  SkPaint third_paint;
673  third_paint.setShader(&third_shader);
674
675  SkPoint points[3];
676  SkColor colors[3];
677  uint16_t indecies[3] = {0, 1, 2};
678  points[0].set(10, 10);
679  points[1].set(100, 20);
680  points[2].set(50, 100);
681  // (10, 10, 90, 90).
682  canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
683                       3,
684                       points,
685                       points,
686                       colors,
687                       NULL,
688                       indecies,
689                       3,
690                       first_paint);
691
692  canvas->save();
693
694  canvas->clipRect(SkRect::MakeWH(50, 50));
695  // (10, 10, 40, 40).
696  canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
697                       3,
698                       points,
699                       points,
700                       colors,
701                       NULL,
702                       indecies,
703                       3,
704                       second_paint);
705
706  canvas->restore();
707
708  points[0].set(50, 55);
709  points[1].set(50, 55);
710  points[2].set(200, 200);
711  // (50, 55, 150, 145).
712  canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
713                       3,
714                       points,
715                       points,
716                       colors,
717                       NULL,
718                       indecies,
719                       3,
720                       third_paint);
721
722  StopRecording(picture.get(), canvas);
723
724  std::vector<skia::LazyPixelRefUtils::PositionLazyPixelRef> pixel_refs;
725  skia::LazyPixelRefUtils::GatherPixelRefs(picture.get(), &pixel_refs);
726
727  EXPECT_EQ(3u, pixel_refs.size());
728  EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 90, 90),
729                       gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect));
730  EXPECT_FLOAT_RECT_EQ(gfx::RectF(10, 10, 40, 40),
731                       gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect));
732  EXPECT_FLOAT_RECT_EQ(gfx::RectF(50, 55, 150, 145),
733                       gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect));
734}
735
736}  // namespace skia
737