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