blob: e5b446390c2e4bffbc062c08cb12ff9158d5aad9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com2ade0862011-03-17 17:48:04 +00008#include "Test.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11
reed@google.com6de0bfc2012-03-30 17:43:33 +000012static void test_nan_antihair(skiatest::Reporter* reporter) {
13 SkBitmap bm;
14 bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
15 bm.allocPixels();
16
17 SkCanvas canvas(bm);
18
19 SkPath path;
20 path.moveTo(0, 0);
21 path.lineTo(10, SK_ScalarNaN);
22
23 SkPaint paint;
24 paint.setAntiAlias(true);
25 paint.setStyle(SkPaint::kStroke_Style);
26
27 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
28 // this would trigger an assert/crash.
29 //
30 // see rev. 3558
31 canvas.drawPath(path, paint);
32}
33
reed@google.com2ade0862011-03-17 17:48:04 +000034static bool check_for_all_zeros(const SkBitmap& bm) {
35 SkAutoLockPixels alp(bm);
36
37 size_t count = bm.width() * bm.bytesPerPixel();
38 for (int y = 0; y < bm.height(); y++) {
39 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
40 for (size_t i = 0; i < count; i++) {
41 if (ptr[i]) {
42 return false;
43 }
44 }
45 }
46 return true;
47}
48
49static const int gWidth = 256;
50static const int gHeight = 256;
51
52static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
53 bm->setConfig(config, gWidth, gHeight);
54 bm->allocPixels();
55 bm->eraseColor(color);
56}
57
58static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
59 SkBitmap src, dst;
60
61 create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
62 create(&dst, SkBitmap::kARGB_8888_Config, 0);
63
64 SkCanvas canvas(dst);
65
66 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
67 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
68
69 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
70
71 // ensure that we draw nothing if srcR does not intersect the bitmap
72 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +000073
74 test_nan_antihair(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +000075}
76
77#include "TestClassDef.h"
78DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)