1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkBlitter.h"
9 #include "SkMask.h"
10 #include "Test.h"
11 #include <string.h>
12 
13 class TestBlitter : public SkBlitter {
14 public:
TestBlitter(SkIRect bounds,skiatest::Reporter * reporter)15     TestBlitter(SkIRect bounds, skiatest::Reporter* reporter)
16         : fBounds(bounds)
17         , fReporter(reporter) { }
18 
blitH(int x,int y,int width)19     void blitH(int x, int y, int width) override {
20 
21         REPORTER_ASSERT(fReporter, x >= fBounds.fLeft && x < fBounds.fRight);
22         REPORTER_ASSERT(fReporter, y >= fBounds.fTop && y < fBounds.fBottom);
23         int right = x + width;
24         REPORTER_ASSERT(fReporter, right > fBounds.fLeft && right <= fBounds.fRight);
25     }
26 
27 private:
28     SkIRect fBounds;
29     skiatest::Reporter* fReporter;
30 };
31 
32 // Exercise all clips compared with different widths of bitMask. Make sure that no buffer
33 // overruns happen.
DEF_TEST(BlitAndClip,reporter)34 DEF_TEST(BlitAndClip, reporter) {
35     const int originX = 100;
36     const int originY = 100;
37     for (int width = 1; width <= 32; width++) {
38         const int height = 2;
39         int rowBytes = (width + 7) >> 3;
40         uint8_t* bits = new uint8_t[rowBytes * height];
41         memset(bits, 0xAA, rowBytes * height);
42 
43         SkIRect b = {originX, originY, originX + width, originY + height};
44 
45         SkMask mask;
46         mask.fFormat = SkMask::kBW_Format;
47         mask.fBounds = b;
48         mask.fImage = (uint8_t*)bits;
49         mask.fRowBytes = rowBytes;
50 
51         TestBlitter tb(mask.fBounds, reporter);
52 
53         for (int top = b.fTop; top < b.fBottom; top++) {
54             for (int bottom = top + 1; bottom <= b.fBottom; bottom++) {
55                 for (int left = b.fLeft; left < b.fRight; left++) {
56                     for (int right = left + 1; right <= b.fRight; right++) {
57                         SkIRect clipRect = {left, top, right, bottom};
58                         tb.blitMask(mask, clipRect);
59                     }
60                 }
61             }
62         }
63 
64         delete [] bits;
65     }
66 }
67