1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TODO(awalker): clean up the const/non-const reference handling in this test
#include "skia/ext/platform_canvas.h"
#include <stdint.h>
#include "base/memory/raw_ref.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPixelRef.h"
// Native drawing context is only used/supported on Windows.
#if BUILDFLAG(IS_WIN)
namespace skia {
namespace {
// Uses DstATop transfer mode with SK_ColorBLACK 0xff000000:
// the destination pixel will end up with 0xff alpha
// if it was transparent black (0x0) before the blend,
// it will be set to opaque black (0xff000000).
// if it has nonzero alpha before the blend,
// it will retain its color.
void MakeOpaque(SkCanvas* canvas, int x, int y, int width, int height) {
if (width <= 0 || height <= 0)
return;
SkRect rect;
rect.setXYWH(SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(width), SkIntToScalar(height));
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setBlendMode(SkBlendMode::kDstATop);
canvas->drawRect(rect, paint);
}
bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) {
// For masking out the alpha values.
constexpr uint32_t alpha_mask = SkColorSetARGB(0xFF, 0, 0, 0);
return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask);
}
// Return true if the canvas is filled to canvas_color, and contains a single
// rectangle filled to rect_color. This function ignores the alpha channel,
// since Windows will sometimes clear the alpha channel when drawing, and we
// will fix that up later in cases it's necessary.
bool VerifyRect(const SkCanvas& canvas,
uint32_t canvas_color, uint32_t rect_color,
int x, int y, int w, int h) {
const SkBitmap bitmap = skia::ReadPixels(const_cast<SkCanvas*>(&canvas));
for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) {
for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) {
if (cur_x >= x && cur_x < x + w &&
cur_y >= y && cur_y < y + h) {
// Inside the square should be rect_color
if (!IsOfColor(bitmap, cur_x, cur_y, rect_color))
return false;
} else {
// Outside the square should be canvas_color
if (!IsOfColor(bitmap, cur_x, cur_y, canvas_color))
return false;
}
}
}
return true;
}
#if !defined(USE_AURA) && !BUILDFLAG(IS_MAC)
// Return true if canvas has something that passes for a rounded-corner
// rectangle. Basically, we're just checking to make sure that the pixels in the
// middle are of rect_color and pixels in the corners are of canvas_color.
bool VerifyRoundedRect(const SkCanvas& canvas,
uint32_t canvas_color,
uint32_t rect_color,
int x,
int y,
int w,
int h) {
SkPixmap pixmap;
ASSERT_TRUE(canvas.peekPixels(&pixmap));
SkBitmap bitmap;
bitmap.installPixels(pixmap);
// Check corner points first. They should be of canvas_color.
if (!IsOfColor(bitmap, x, y, canvas_color)) return false;
if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false;
if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false;
if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false;
// Check middle points. They should be of rect_color.
if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false;
if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false;
if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false;
if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false;
return true;
}
#endif
// Checks whether there is a white canvas with a black square at the given
// location in pixels (not in the canvas coordinate system).
bool VerifyBlackRect(const SkCanvas& canvas, int x, int y, int w, int h) {
return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h);
}
// Check that every pixel in the canvas is a single color.
bool VerifyCanvasColor(const SkCanvas& canvas, uint32_t canvas_color) {
return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0);
}
void DrawNativeRect(SkCanvas& canvas, int x, int y, int w, int h) {
HDC dc = skia::GetNativeDrawingContext(&canvas);
RECT inner_rc;
inner_rc.left = x;
inner_rc.top = y;
inner_rc.right = x + w;
inner_rc.bottom = y + h;
FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
}
// Clips the contents of the canvas to the given rectangle. This will be
// intersected with any existing clip.
void AddClip(SkCanvas& canvas, int x, int y, int w, int h) {
SkRect rect;
rect.setXYWH(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(w),
SkIntToScalar(h));
canvas.clipRect(rect);
}
class LayerSaver {
public:
LayerSaver(SkCanvas& canvas, int x, int y, int w, int h)
: canvas_(canvas),
x_(x),
y_(y),
w_(w),
h_(h) {
SkRect bounds;
bounds.setLTRB(SkIntToScalar(x_), SkIntToScalar(y_), SkIntToScalar(right()),
SkIntToScalar(bottom()));
canvas_->saveLayer(&bounds, NULL);
canvas.clear(SkColorSetARGB(0, 0, 0, 0));
}
~LayerSaver() { canvas_->restore(); }
int x() const { return x_; }
int y() const { return y_; }
int w() const { return w_; }
int h() const { return h_; }
// Returns the EXCLUSIVE far bounds of the layer.
int right() const { return x_ + w_; }
int bottom() const { return y_ + h_; }
private:
const raw_ref<SkCanvas> canvas_;
int x_, y_, w_, h_;
};
// Size used for making layers in many of the below tests.
const int kLayerX = 2;
const int kLayerY = 3;
const int kLayerW = 9;
const int kLayerH = 7;
// Size used by some tests to draw a rectangle inside the layer.
const int kInnerX = 4;
const int kInnerY = 5;
const int kInnerW = 2;
const int kInnerH = 3;
}
// This just checks that our checking code is working properly, it just uses
// regular skia primitives.
TEST(PlatformCanvas, SkLayer) {
// Create the canvas initialized to opaque white.
std::unique_ptr<SkCanvas> canvas = CreatePlatformCanvas(16, 16, true);
canvas->drawColor(SK_ColorWHITE);
// Make a layer and fill it completely to make sure that the bounds are
// correct.
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
canvas->drawColor(SK_ColorBLACK);
}
EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
}
// Test native clipping.
TEST(PlatformCanvas, ClipRegion) {
// Initialize a white canvas
std::unique_ptr<SkCanvas> canvas = CreatePlatformCanvas(16, 16, true);
canvas->drawColor(SK_ColorWHITE);
EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
// Test that initially the canvas has no clip region, by filling it
// with a black rectangle.
// Note: Don't use LayerSaver, since internally it sets a clip region.
DrawNativeRect(*canvas, 0, 0, 16, 16);
EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK));
// Test that intersecting disjoint clip rectangles sets an empty clip region
canvas->drawColor(SK_ColorWHITE);
EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
{
LayerSaver layer(*canvas, 0, 0, 16, 16);
AddClip(*canvas, 2, 3, 4, 5);
AddClip(*canvas, 4, 9, 10, 10);
DrawNativeRect(*canvas, 0, 0, 16, 16);
}
EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
}
// Test the layers get filled properly by native rendering.
TEST(PlatformCanvas, FillLayer) {
// Create the canvas initialized to opaque white.
std::unique_ptr<SkCanvas> canvas(CreatePlatformCanvas(16, 16, true));
// Make a layer and fill it completely to make sure that the bounds are
// correct.
canvas->drawColor(SK_ColorWHITE);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
DrawNativeRect(*canvas, 0, 0, 100, 100);
MakeOpaque(canvas.get(), 0, 0, 100, 100);
}
EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
// Make a layer and fill it partially to make sure the translation is correct.
canvas->drawColor(SK_ColorWHITE);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
}
EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
// Add a clip on the layer and fill to make sure clip is correct.
canvas->drawColor(SK_ColorWHITE);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
canvas->save();
AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
DrawNativeRect(*canvas, 0, 0, 100, 100);
MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
canvas->restore();
}
EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
// Add a clip and then make the layer to make sure the clip is correct.
canvas->drawColor(SK_ColorWHITE);
canvas->save();
AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
DrawNativeRect(*canvas, 0, 0, 100, 100);
MakeOpaque(canvas.get(), 0, 0, 100, 100);
}
canvas->restore();
EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
}
// Test that translation + make layer works properly.
TEST(PlatformCanvas, TranslateLayer) {
// Create the canvas initialized to opaque white.
std::unique_ptr<SkCanvas> canvas = CreatePlatformCanvas(16, 16, true);
// Make a layer and fill it completely to make sure that the bounds are
// correct.
canvas->drawColor(SK_ColorWHITE);
canvas->save();
canvas->translate(1, 1);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
DrawNativeRect(*canvas, 0, 0, 100, 100);
MakeOpaque(canvas.get(), 0, 0, 100, 100);
}
canvas->restore();
EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1,
kLayerW, kLayerH));
// Translate then make the layer.
canvas->drawColor(SK_ColorWHITE);
canvas->save();
canvas->translate(1, 1);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
}
canvas->restore();
EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
kInnerW, kInnerH));
// Make the layer then translate.
canvas->drawColor(SK_ColorWHITE);
canvas->save();
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
canvas->translate(1, 1);
DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
}
canvas->restore();
EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
kInnerW, kInnerH));
// Translate both before and after, and have a clip.
canvas->drawColor(SK_ColorWHITE);
canvas->save();
canvas->translate(1, 1);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
canvas->drawColor(SK_ColorWHITE);
canvas->translate(1, 1);
AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1);
DrawNativeRect(*canvas, 0, 0, 100, 100);
MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
}
canvas->restore();
EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3,
kInnerW - 1, kInnerH - 1));
// TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?),
// modify test and remove this guard.
#if !defined(USE_AURA)
// Translate both before and after, and have a path clip.
canvas->drawColor(SK_ColorWHITE);
canvas->save();
canvas->translate(1, 1);
{
LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
canvas->drawColor(SK_ColorWHITE);
canvas->translate(1, 1);
SkPath path;
SkRect rect;
rect.iset(kInnerX - 1, kInnerY - 1,
kInnerX + kInnerW, kInnerY + kInnerH);
const SkScalar kRadius = 2.0;
path.addRoundRect(rect, kRadius, kRadius);
canvas->clipPath(path);
DrawNativeRect(*canvas, 0, 0, 100, 100);
MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
}
canvas->restore();
EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK,
kInnerX + 1, kInnerY + 1, kInnerW, kInnerH));
#endif
}
} // namespace skia
#endif // BUILDFLAG(IS_WIN)
|