| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
| 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE | 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE |
| 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 23 * DAMAGE. | 23 * DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "platform/graphics/GraphicsContext.h" | 26 #include "platform/graphics/GraphicsContext.h" |
| 27 | 27 |
| 28 #include <memory> |
| 28 #include "platform/graphics/BitmapImage.h" | 29 #include "platform/graphics/BitmapImage.h" |
| 29 #include "platform/graphics/Path.h" | 30 #include "platform/graphics/Path.h" |
| 30 #include "platform/graphics/paint/PaintController.h" | 31 #include "platform/graphics/paint/PaintController.h" |
| 31 #include "platform/graphics/paint/PaintRecord.h" | 32 #include "platform/graphics/paint/PaintRecord.h" |
| 32 #include "platform/testing/FontTestHelpers.h" | 33 #include "platform/testing/FontTestHelpers.h" |
| 33 #include "platform/testing/UnitTestHelpers.h" | 34 #include "platform/testing/UnitTestHelpers.h" |
| 34 #include "platform/text/TextRun.h" | 35 #include "platform/text/TextRun.h" |
| 36 #include "platform/wtf/PtrUtil.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "third_party/skia/include/core/SkBitmap.h" | 38 #include "third_party/skia/include/core/SkBitmap.h" |
| 37 #include "third_party/skia/include/core/SkCanvas.h" | 39 #include "third_party/skia/include/core/SkCanvas.h" |
| 38 #include "third_party/skia/include/core/SkShader.h" | 40 #include "third_party/skia/include/core/SkShader.h" |
| 39 #include <memory> | |
| 40 | 41 |
| 41 namespace blink { | 42 namespace blink { |
| 42 | 43 |
| 43 #define EXPECT_EQ_RECT(a, b) \ | 44 #define EXPECT_EQ_RECT(a, b) \ |
| 44 EXPECT_EQ(a.x(), b.x()); \ | 45 EXPECT_EQ(a.x(), b.x()); \ |
| 45 EXPECT_EQ(a.y(), b.y()); \ | 46 EXPECT_EQ(a.y(), b.y()); \ |
| 46 EXPECT_EQ(a.width(), b.width()); \ | 47 EXPECT_EQ(a.width(), b.width()); \ |
| 47 EXPECT_EQ(a.height(), b.height()); | 48 EXPECT_EQ(a.height(), b.height()); |
| 48 | 49 |
| 49 #define EXPECT_OPAQUE_PIXELS_IN_RECT(bitmap, opaqueRect) \ | 50 #define EXPECT_OPAQUE_PIXELS_IN_RECT(bitmap, opaqueRect) \ |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 path.AddLineTo(FloatPoint(40, 40)); | 135 path.AddLineTo(FloatPoint(40, 40)); |
| 135 PaintFlags flags; | 136 PaintFlags flags; |
| 136 flags.setColor(alpha.Rgb()); | 137 flags.setColor(alpha.Rgb()); |
| 137 flags.setBlendMode(SkBlendMode::kSrcOut); | 138 flags.setBlendMode(SkBlendMode::kSrcOut); |
| 138 context.DrawPath(path.GetSkPath(), flags); | 139 context.DrawPath(path.GetSkPath(), flags); |
| 139 | 140 |
| 140 canvas.drawPicture(context.EndRecording()); | 141 canvas.drawPicture(context.EndRecording()); |
| 141 EXPECT_OPAQUE_PIXELS_IN_RECT(bitmap, IntRect(20, 10, 30, 40)); | 142 EXPECT_OPAQUE_PIXELS_IN_RECT(bitmap, IntRect(20, 10, 30, 40)); |
| 142 } | 143 } |
| 143 | 144 |
| 145 class GraphicsContextHighConstrastTest : public ::testing::Test { |
| 146 protected: |
| 147 void SetUp() override { |
| 148 bitmap_.allocN32Pixels(4, 1); |
| 149 bitmap_.eraseColor(0); |
| 150 canvas_ = WTF::WrapUnique(new SkiaPaintCanvas(bitmap_)); |
| 151 paint_controller_ = PaintController::Create(); |
| 152 context_ = WTF::WrapUnique(new GraphicsContext(*paint_controller_)); |
| 153 context_->BeginRecording(FloatRect(0, 0, 4, 1)); |
| 154 } |
| 155 |
| 156 void DrawColorsToContext() { |
| 157 Color black(0.0f, 0.0f, 0.0f, 1.0f); |
| 158 Color white(1.0f, 1.0f, 1.0f, 1.0f); |
| 159 Color red(1.0f, 0.0f, 0.0f, 1.0f); |
| 160 Color gray(0.5f, 0.5f, 0.5f, 1.0f); |
| 161 context_->FillRect(FloatRect(0, 0, 1, 1), black); |
| 162 context_->FillRect(FloatRect(1, 0, 1, 1), white); |
| 163 context_->FillRect(FloatRect(2, 0, 1, 1), red); |
| 164 context_->FillRect(FloatRect(3, 0, 1, 1), gray); |
| 165 // Capture the result in the bitmap. |
| 166 canvas_->drawPicture(context_->EndRecording()); |
| 167 } |
| 168 |
| 169 SkBitmap bitmap_; |
| 170 std::unique_ptr<SkiaPaintCanvas> canvas_; |
| 171 std::unique_ptr<PaintController> paint_controller_; |
| 172 std::unique_ptr<GraphicsContext> context_; |
| 173 }; |
| 174 |
| 175 // This is just a baseline test, compare against the other variants |
| 176 // of the test below, where high contrast mode is enabled. |
| 177 TEST_F(GraphicsContextHighConstrastTest, NoHighContrast) { |
| 178 DrawColorsToContext(); |
| 179 |
| 180 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(0, 0)); |
| 181 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(1, 0)); |
| 182 EXPECT_EQ(0xffff0000, *bitmap_.getAddr32(2, 0)); |
| 183 EXPECT_EQ(0xff808080, *bitmap_.getAddr32(3, 0)); |
| 184 } |
| 185 |
| 186 TEST_F(GraphicsContextHighConstrastTest, HighContrastOff) { |
| 187 HighContrastSettings settings; |
| 188 settings.mode = HighContrastMode::kOff; |
| 189 settings.grayscale = false; |
| 190 settings.contrast = 0; |
| 191 context_->SetHighContrast(settings); |
| 192 |
| 193 DrawColorsToContext(); |
| 194 |
| 195 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(0, 0)); |
| 196 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(1, 0)); |
| 197 EXPECT_EQ(0xffff0000, *bitmap_.getAddr32(2, 0)); |
| 198 EXPECT_EQ(0xff808080, *bitmap_.getAddr32(3, 0)); |
| 199 } |
| 200 |
| 201 // Simple invert for testing. Each color component |c| |
| 202 // is replaced with |255 - c| for easy testing. |
| 203 TEST_F(GraphicsContextHighConstrastTest, SimpleInvertForTesting) { |
| 204 HighContrastSettings settings; |
| 205 settings.mode = HighContrastMode::kSimpleInvertForTesting; |
| 206 settings.grayscale = false; |
| 207 settings.contrast = 0; |
| 208 context_->SetHighContrast(settings); |
| 209 |
| 210 DrawColorsToContext(); |
| 211 |
| 212 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(0, 0)); |
| 213 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(1, 0)); |
| 214 EXPECT_EQ(0xff00ffff, *bitmap_.getAddr32(2, 0)); |
| 215 EXPECT_EQ(0xff7f7f7f, *bitmap_.getAddr32(3, 0)); |
| 216 } |
| 217 |
| 218 // Invert brightness (with gamma correction). |
| 219 TEST_F(GraphicsContextHighConstrastTest, InvertBrightness) { |
| 220 HighContrastSettings settings; |
| 221 settings.mode = HighContrastMode::kInvertBrightness; |
| 222 settings.grayscale = false; |
| 223 settings.contrast = 0; |
| 224 context_->SetHighContrast(settings); |
| 225 |
| 226 DrawColorsToContext(); |
| 227 |
| 228 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(0, 0)); |
| 229 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(1, 0)); |
| 230 EXPECT_EQ(0xff00ffff, *bitmap_.getAddr32(2, 0)); |
| 231 EXPECT_EQ(0xffdddddd, *bitmap_.getAddr32(3, 0)); |
| 232 } |
| 233 |
| 234 // Invert lightness (in HSL space). |
| 235 TEST_F(GraphicsContextHighConstrastTest, InvertLightness) { |
| 236 HighContrastSettings settings; |
| 237 settings.mode = HighContrastMode::kInvertLightness; |
| 238 settings.grayscale = false; |
| 239 settings.contrast = 0; |
| 240 context_->SetHighContrast(settings); |
| 241 |
| 242 DrawColorsToContext(); |
| 243 |
| 244 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(0, 0)); |
| 245 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(1, 0)); |
| 246 EXPECT_EQ(0xffff0000, *bitmap_.getAddr32(2, 0)); |
| 247 EXPECT_EQ(0xffdddddd, *bitmap_.getAddr32(3, 0)); |
| 248 } |
| 249 |
| 250 // Invert lightness plus grayscale. |
| 251 TEST_F(GraphicsContextHighConstrastTest, InvertLightnessPlusGrayscale) { |
| 252 HighContrastSettings settings; |
| 253 settings.mode = HighContrastMode::kInvertLightness; |
| 254 settings.grayscale = true; |
| 255 settings.contrast = 0; |
| 256 context_->SetHighContrast(settings); |
| 257 |
| 258 DrawColorsToContext(); |
| 259 |
| 260 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(0, 0)); |
| 261 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(1, 0)); |
| 262 EXPECT_EQ(0xffe2e2e2, *bitmap_.getAddr32(2, 0)); |
| 263 EXPECT_EQ(0xffdddddd, *bitmap_.getAddr32(3, 0)); |
| 264 } |
| 265 |
| 266 TEST_F(GraphicsContextHighConstrastTest, InvertLightnessPlusContrast) { |
| 267 HighContrastSettings settings; |
| 268 settings.mode = HighContrastMode::kInvertLightness; |
| 269 settings.grayscale = false; |
| 270 settings.contrast = 0.2; |
| 271 context_->SetHighContrast(settings); |
| 272 |
| 273 DrawColorsToContext(); |
| 274 |
| 275 EXPECT_EQ(0xffffffff, *bitmap_.getAddr32(0, 0)); |
| 276 EXPECT_EQ(0xff000000, *bitmap_.getAddr32(1, 0)); |
| 277 EXPECT_EQ(0xffff0000, *bitmap_.getAddr32(2, 0)); |
| 278 EXPECT_EQ(0xffeeeeee, *bitmap_.getAddr32(3, 0)); |
| 279 } |
| 280 |
| 144 } // namespace blink | 281 } // namespace blink |
| OLD | NEW |