| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // TODO(awalker): clean up the const/non-const reference handling in this test | 5 // TODO(awalker): clean up the const/non-const reference handling in this test |
| 6 | 6 |
| 7 #include "skia/ext/platform_canvas.h" | 7 #include "skia/ext/platform_canvas.h" |
| 8 | 8 |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "skia/ext/platform_device.h" | 13 #include "skia/ext/platform_device.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "third_party/skia/include/core/SkCanvas.h" |
| 17 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
| 18 #include "third_party/skia/include/core/SkColorPriv.h" | 18 #include "third_party/skia/include/core/SkColorPriv.h" |
| 19 #include "third_party/skia/include/core/SkPixelRef.h" | 19 #include "third_party/skia/include/core/SkPixelRef.h" |
| 20 | 20 |
| 21 #if defined(OS_MACOSX) | 21 // Native drawing context is only used/supported on Windows. |
| 22 #import <ApplicationServices/ApplicationServices.h> | 22 #if defined(OS_WIN) |
| 23 #endif | |
| 24 | |
| 25 #if !defined(OS_WIN) | |
| 26 #include <unistd.h> | |
| 27 #endif | |
| 28 | |
| 29 #if defined(USE_CAIRO) | |
| 30 #if defined(OS_OPENBSD) | |
| 31 #include <cairo.h> | |
| 32 #else | |
| 33 #include <cairo/cairo.h> | |
| 34 #endif // OS_OPENBSD | |
| 35 #endif // USE_CAIRO | |
| 36 | 23 |
| 37 namespace skia { | 24 namespace skia { |
| 38 | 25 |
| 39 namespace { | 26 namespace { |
| 40 | 27 |
| 41 // Uses DstATop transfer mode with SK_ColorBLACK 0xff000000: | 28 // Uses DstATop transfer mode with SK_ColorBLACK 0xff000000: |
| 42 // the destination pixel will end up with 0xff alpha | 29 // the destination pixel will end up with 0xff alpha |
| 43 // if it was transparent black (0x0) before the blend, | 30 // if it was transparent black (0x0) before the blend, |
| 44 // it will be set to opaque black (0xff000000). | 31 // it will be set to opaque black (0xff000000). |
| 45 // if it has nonzero alpha before the blend, | 32 // if it has nonzero alpha before the blend, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 // location in pixels (not in the canvas coordinate system). | 114 // location in pixels (not in the canvas coordinate system). |
| 128 bool VerifyBlackRect(const SkCanvas& canvas, int x, int y, int w, int h) { | 115 bool VerifyBlackRect(const SkCanvas& canvas, int x, int y, int w, int h) { |
| 129 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); | 116 return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h); |
| 130 } | 117 } |
| 131 | 118 |
| 132 // Check that every pixel in the canvas is a single color. | 119 // Check that every pixel in the canvas is a single color. |
| 133 bool VerifyCanvasColor(const SkCanvas& canvas, uint32_t canvas_color) { | 120 bool VerifyCanvasColor(const SkCanvas& canvas, uint32_t canvas_color) { |
| 134 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); | 121 return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0); |
| 135 } | 122 } |
| 136 | 123 |
| 137 #if defined(OS_WIN) | |
| 138 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | 124 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { |
| 139 HDC dc = skia::GetNativeDrawingContext(&canvas); | 125 HDC dc = skia::GetNativeDrawingContext(&canvas); |
| 140 | 126 |
| 141 RECT inner_rc; | 127 RECT inner_rc; |
| 142 inner_rc.left = x; | 128 inner_rc.left = x; |
| 143 inner_rc.top = y; | 129 inner_rc.top = y; |
| 144 inner_rc.right = x + w; | 130 inner_rc.right = x + w; |
| 145 inner_rc.bottom = y + h; | 131 inner_rc.bottom = y + h; |
| 146 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; | 132 FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)))
; |
| 147 } | 133 } |
| 148 #elif defined(OS_MACOSX) | |
| 149 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | |
| 150 CGContextRef context = skia::GetNativeDrawingContext(&canvas); | |
| 151 | |
| 152 CGRect inner_rc = CGRectMake(x, y, w, h); | |
| 153 // RGBA opaque black | |
| 154 CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0); | |
| 155 CGContextSetFillColorWithColor(context, black); | |
| 156 CGColorRelease(black); | |
| 157 CGContextFillRect(context, inner_rc); | |
| 158 } | |
| 159 #elif defined(USE_CAIRO) | |
| 160 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | |
| 161 cairo_t* context = skia::GetNativeDrawingContext(&canvas); | |
| 162 | |
| 163 cairo_rectangle(context, x, y, w, h); | |
| 164 cairo_set_source_rgb(context, 0.0, 0.0, 0.0); | |
| 165 cairo_fill(context); | |
| 166 } | |
| 167 #else | |
| 168 void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) { | |
| 169 NOTIMPLEMENTED(); | |
| 170 } | |
| 171 #endif | |
| 172 | 134 |
| 173 // Clips the contents of the canvas to the given rectangle. This will be | 135 // Clips the contents of the canvas to the given rectangle. This will be |
| 174 // intersected with any existing clip. | 136 // intersected with any existing clip. |
| 175 void AddClip(SkCanvas& canvas, int x, int y, int w, int h) { | 137 void AddClip(SkCanvas& canvas, int x, int y, int w, int h) { |
| 176 SkRect rect; | 138 SkRect rect; |
| 177 rect.set(SkIntToScalar(x), SkIntToScalar(y), | 139 rect.set(SkIntToScalar(x), SkIntToScalar(y), |
| 178 SkIntToScalar(x + w), SkIntToScalar(y + h)); | 140 SkIntToScalar(x + w), SkIntToScalar(y + h)); |
| 179 canvas.clipRect(rect); | 141 canvas.clipRect(rect); |
| 180 } | 142 } |
| 181 | 143 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1); | 334 AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1); |
| 373 DrawNativeRect(*canvas, 0, 0, 100, 100); | 335 DrawNativeRect(*canvas, 0, 0, 100, 100); |
| 374 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); | 336 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); |
| 375 } | 337 } |
| 376 canvas->restore(); | 338 canvas->restore(); |
| 377 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3, | 339 EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3, |
| 378 kInnerW - 1, kInnerH - 1)); | 340 kInnerW - 1, kInnerH - 1)); |
| 379 | 341 |
| 380 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?), | 342 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?), |
| 381 // modify test and remove this guard. | 343 // modify test and remove this guard. |
| 382 #if !defined(OS_MACOSX) && !defined(USE_AURA) | 344 #if !defined(USE_AURA) |
| 383 // Translate both before and after, and have a path clip. | 345 // Translate both before and after, and have a path clip. |
| 384 canvas->drawColor(SK_ColorWHITE); | 346 canvas->drawColor(SK_ColorWHITE); |
| 385 canvas->save(); | 347 canvas->save(); |
| 386 canvas->translate(1, 1); | 348 canvas->translate(1, 1); |
| 387 { | 349 { |
| 388 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); | 350 LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH); |
| 389 canvas->drawColor(SK_ColorWHITE); | 351 canvas->drawColor(SK_ColorWHITE); |
| 390 canvas->translate(1, 1); | 352 canvas->translate(1, 1); |
| 391 | 353 |
| 392 SkPath path; | 354 SkPath path; |
| 393 SkRect rect; | 355 SkRect rect; |
| 394 rect.iset(kInnerX - 1, kInnerY - 1, | 356 rect.iset(kInnerX - 1, kInnerY - 1, |
| 395 kInnerX + kInnerW, kInnerY + kInnerH); | 357 kInnerX + kInnerW, kInnerY + kInnerH); |
| 396 const SkScalar kRadius = 2.0; | 358 const SkScalar kRadius = 2.0; |
| 397 path.addRoundRect(rect, kRadius, kRadius); | 359 path.addRoundRect(rect, kRadius, kRadius); |
| 398 canvas->clipPath(path); | 360 canvas->clipPath(path); |
| 399 | 361 |
| 400 DrawNativeRect(*canvas, 0, 0, 100, 100); | 362 DrawNativeRect(*canvas, 0, 0, 100, 100); |
| 401 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); | 363 MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH); |
| 402 } | 364 } |
| 403 canvas->restore(); | 365 canvas->restore(); |
| 404 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, | 366 EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK, |
| 405 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); | 367 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH)); |
| 406 #endif | 368 #endif |
| 407 } | 369 } |
| 408 | 370 |
| 409 } // namespace skia | 371 } // namespace skia |
| 372 |
| 373 #endif // defined(OS_WIN) |
| OLD | NEW |