• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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/basictypes.h"
6 #include "base/memory/shared_memory.h"
7 #include "skia/ext/platform_canvas.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/blit.h"
10 #include "ui/gfx/point.h"
11 #include "ui/gfx/rect.h"
12 
13 namespace {
14 
15 // Fills the given canvas with the values by duplicating the values into each
16 // color channel for the corresponding pixel.
17 //
18 // Example values = {{0x0, 0x01}, {0x12, 0xFF}} would give a canvas with:
19 //   0x00000000 0x01010101
20 //   0x12121212 0xFFFFFFFF
21 template<int w, int h>
SetToCanvas(skia::PlatformCanvas * canvas,uint8 values[h][w])22 void SetToCanvas(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
23   SkBitmap& bitmap = const_cast<SkBitmap&>(
24       skia::GetTopDevice(*canvas)->accessBitmap(true));
25   SkAutoLockPixels lock(bitmap);
26   ASSERT_EQ(w, bitmap.width());
27   ASSERT_EQ(h, bitmap.height());
28 
29   for (int y = 0; y < h; y++) {
30     for (int x = 0; x < w; x++) {
31       uint8 value = values[y][x];
32       *bitmap.getAddr32(x, y) =
33           (value << 24) | (value << 16) | (value << 8) | value;
34     }
35   }
36 }
37 
38 // Checks each pixel in the given canvas and see if it is made up of the given
39 // values, where each value has been duplicated into each channel of the given
40 // bitmap (see SetToCanvas above).
41 template<int w, int h>
VerifyCanvasValues(skia::PlatformCanvas * canvas,uint8 values[h][w])42 void VerifyCanvasValues(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
43   SkBitmap& bitmap = const_cast<SkBitmap&>(
44       skia::GetTopDevice(*canvas)->accessBitmap(true));
45   SkAutoLockPixels lock(bitmap);
46   ASSERT_EQ(w, bitmap.width());
47   ASSERT_EQ(h, bitmap.height());
48 
49   for (int y = 0; y < h; y++) {
50     for (int x = 0; x < w; x++) {
51       uint8 value = values[y][x];
52       uint32 expected =
53           (value << 24) | (value << 16) | (value << 8) | value;
54       ASSERT_EQ(expected, *bitmap.getAddr32(x, y));
55     }
56   }
57 }
58 
59 }  // namespace
60 
TEST(Blit,ScrollCanvas)61 TEST(Blit, ScrollCanvas) {
62   static const int kCanvasWidth = 5;
63   static const int kCanvasHeight = 5;
64   skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
65       skia::CreatePlatformCanvas(kCanvasWidth, kCanvasHeight, true));
66   uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
67       { 0x00, 0x01, 0x02, 0x03, 0x04 },
68       { 0x10, 0x11, 0x12, 0x13, 0x14 },
69       { 0x20, 0x21, 0x22, 0x23, 0x24 },
70       { 0x30, 0x31, 0x32, 0x33, 0x34 },
71       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
72   SetToCanvas<5, 5>(canvas.get(), initial_values);
73 
74   // Sanity check on input.
75   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
76 
77   // Scroll none and make sure it's a NOP.
78   gfx::ScrollCanvas(canvas.get(),
79                     gfx::Rect(0, 0, kCanvasWidth, kCanvasHeight),
80                     gfx::Vector2d(0, 0));
81   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
82 
83   // Scroll with a empty clip and make sure it's a NOP.
84   gfx::Rect empty_clip(1, 1, 0, 0);
85   gfx::ScrollCanvas(canvas.get(), empty_clip, gfx::Vector2d(0, 1));
86   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
87 
88   // Scroll the center 3 pixels up one.
89   gfx::Rect center_three(1, 1, 3, 3);
90   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(0, -1));
91   uint8 scroll_up_expected[kCanvasHeight][kCanvasWidth] = {
92       { 0x00, 0x01, 0x02, 0x03, 0x04 },
93       { 0x10, 0x21, 0x22, 0x23, 0x14 },
94       { 0x20, 0x31, 0x32, 0x33, 0x24 },
95       { 0x30, 0x31, 0x32, 0x33, 0x34 },
96       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
97   VerifyCanvasValues<5, 5>(canvas.get(), scroll_up_expected);
98 
99   // Reset and scroll the center 3 pixels down one.
100   SetToCanvas<5, 5>(canvas.get(), initial_values);
101   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(0, 1));
102   uint8 scroll_down_expected[kCanvasHeight][kCanvasWidth] = {
103       { 0x00, 0x01, 0x02, 0x03, 0x04 },
104       { 0x10, 0x11, 0x12, 0x13, 0x14 },
105       { 0x20, 0x11, 0x12, 0x13, 0x24 },
106       { 0x30, 0x21, 0x22, 0x23, 0x34 },
107       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
108   VerifyCanvasValues<5, 5>(canvas.get(), scroll_down_expected);
109 
110   // Reset and scroll the center 3 pixels right one.
111   SetToCanvas<5, 5>(canvas.get(), initial_values);
112   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(1, 0));
113   uint8 scroll_right_expected[kCanvasHeight][kCanvasWidth] = {
114       { 0x00, 0x01, 0x02, 0x03, 0x04 },
115       { 0x10, 0x11, 0x11, 0x12, 0x14 },
116       { 0x20, 0x21, 0x21, 0x22, 0x24 },
117       { 0x30, 0x31, 0x31, 0x32, 0x34 },
118       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
119   VerifyCanvasValues<5, 5>(canvas.get(), scroll_right_expected);
120 
121   // Reset and scroll the center 3 pixels left one.
122   SetToCanvas<5, 5>(canvas.get(), initial_values);
123   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(-1, 0));
124   uint8 scroll_left_expected[kCanvasHeight][kCanvasWidth] = {
125       { 0x00, 0x01, 0x02, 0x03, 0x04 },
126       { 0x10, 0x12, 0x13, 0x13, 0x14 },
127       { 0x20, 0x22, 0x23, 0x23, 0x24 },
128       { 0x30, 0x32, 0x33, 0x33, 0x34 },
129       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
130   VerifyCanvasValues<5, 5>(canvas.get(), scroll_left_expected);
131 
132   // Diagonal scroll.
133   SetToCanvas<5, 5>(canvas.get(), initial_values);
134   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(2, 2));
135   uint8 scroll_diagonal_expected[kCanvasHeight][kCanvasWidth] = {
136       { 0x00, 0x01, 0x02, 0x03, 0x04 },
137       { 0x10, 0x11, 0x12, 0x13, 0x14 },
138       { 0x20, 0x21, 0x22, 0x23, 0x24 },
139       { 0x30, 0x31, 0x32, 0x11, 0x34 },
140       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
141   VerifyCanvasValues<5, 5>(canvas.get(), scroll_diagonal_expected);
142 }
143 
144 #if defined(OS_WIN)
145 
TEST(Blit,WithSharedMemory)146 TEST(Blit, WithSharedMemory) {
147   const int kCanvasWidth = 5;
148   const int kCanvasHeight = 5;
149   base::SharedMemory shared_mem;
150   ASSERT_TRUE(shared_mem.CreateAnonymous(kCanvasWidth * kCanvasHeight));
151   base::SharedMemoryHandle section = shared_mem.handle();
152   skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
153       skia::CreatePlatformCanvas(kCanvasWidth, kCanvasHeight, true, section,
154                                  skia::RETURN_NULL_ON_FAILURE));
155   ASSERT_TRUE(canvas);
156   shared_mem.Close();
157 
158   uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
159       { 0x00, 0x01, 0x02, 0x03, 0x04 },
160       { 0x10, 0x11, 0x12, 0x13, 0x14 },
161       { 0x20, 0x21, 0x22, 0x23, 0x24 },
162       { 0x30, 0x31, 0x32, 0x33, 0x34 },
163       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
164   SetToCanvas<5, 5>(canvas.get(), initial_values);
165 
166   // Sanity check on input.
167   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
168 }
169 
170 #endif
171 
172