• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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 "gm.h"
9 #include "SkCanvas.h"
10 #include "SkGradientShader.h"
11 #include "SkGraphics.h"
12 #include "SkPath.h"
13 #include "SkRegion.h"
14 #include "SkShader.h"
15 
make_bitmap(SkBitmap * bitmap)16 static void make_bitmap(SkBitmap* bitmap) {
17     bitmap->allocN32Pixels(64, 64);
18 
19     SkCanvas canvas(*bitmap);
20 
21     canvas.drawColor(SK_ColorRED);
22     SkPaint paint;
23     paint.setAntiAlias(true);
24     const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
25     const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
26     paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
27                                                  SkShader::kClamp_TileMode));
28     canvas.drawCircle(32, 32, 32, paint);
29 }
30 
31 class DrawBitmapRect2 : public skiagm::GM {
32     bool fUseIRect;
33 public:
DrawBitmapRect2(bool useIRect)34     DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
35     }
36 
37 protected:
onShortName()38     SkString onShortName() override {
39         SkString str;
40         str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
41         return str;
42     }
43 
onISize()44     SkISize onISize() override {
45         return SkISize::Make(640, 480);
46     }
47 
onDraw(SkCanvas * canvas)48     void onDraw(SkCanvas* canvas) override {
49         canvas->drawColor(0xFFCCCCCC);
50 
51         const SkIRect src[] = {
52             { 0, 0, 32, 32 },
53             { 0, 0, 80, 80 },
54             { 32, 32, 96, 96 },
55             { -32, -32, 32, 32, }
56         };
57 
58         SkPaint paint;
59         paint.setStyle(SkPaint::kStroke_Style);
60 
61         SkBitmap bitmap;
62         make_bitmap(&bitmap);
63 
64         SkRect dstR = { 0, 200, 128, 380 };
65 
66         canvas->translate(16, 40);
67         for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
68             SkRect srcR;
69             srcR.set(src[i]);
70 
71             canvas->drawBitmap(bitmap, 0, 0, &paint);
72             if (!fUseIRect) {
73                 canvas->drawBitmapRect(bitmap, srcR, dstR, &paint,
74                                        SkCanvas::kStrict_SrcRectConstraint);
75             } else {
76                 canvas->drawBitmapRect(bitmap, src[i], dstR, &paint);
77             }
78 
79             canvas->drawRect(dstR, paint);
80             canvas->drawRect(srcR, paint);
81 
82             canvas->translate(160, 0);
83         }
84     }
85 
86 private:
87     typedef skiagm::GM INHERITED;
88 };
89 
90 //////////////////////////////////////////////////////////////////////////////
91 
make_3x3_bitmap(SkBitmap * bitmap)92 static void make_3x3_bitmap(SkBitmap* bitmap) {
93     const int xSize = 3;
94     const int ySize = 3;
95 
96     const SkColor textureData[xSize][ySize] = {
97         { SK_ColorRED,    SK_ColorWHITE, SK_ColorBLUE },
98         { SK_ColorGREEN,  SK_ColorBLACK, SK_ColorCYAN },
99         { SK_ColorYELLOW, SK_ColorGRAY,  SK_ColorMAGENTA }
100     };
101 
102     bitmap->allocN32Pixels(xSize, ySize, true);
103     SkCanvas canvas(*bitmap);
104     SkPaint paint;
105 
106     for (int y = 0; y < ySize; y++) {
107         for (int x = 0; x < xSize; x++) {
108             paint.setColor(textureData[x][y]);
109             canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
110         }
111     }
112 }
113 
114 // This GM attempts to make visible any issues drawBitmapRect may have
115 // with partial source rects. In this case the eight pixels on the border
116 // should be half the width/height of the central pixel, i.e.:
117 //                         __|____|__
118 //                           |    |
119 //                         __|____|__
120 //                           |    |
121 class DrawBitmapRect3 : public skiagm::GM {
122 public:
DrawBitmapRect3()123     DrawBitmapRect3() {
124         this->setBGColor(SK_ColorBLACK);
125     }
126 
127 protected:
onShortName()128     SkString onShortName() override {
129         SkString str;
130         str.printf("3x3bitmaprect");
131         return str;
132     }
133 
onISize()134     SkISize onISize() override {
135         return SkISize::Make(640, 480);
136     }
137 
onDraw(SkCanvas * canvas)138     void onDraw(SkCanvas* canvas) override {
139 
140         SkBitmap bitmap;
141         make_3x3_bitmap(&bitmap);
142 
143         SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
144         SkRect dstR = { 100, 100, 300, 200 };
145 
146         canvas->drawBitmapRect(bitmap, srcR, dstR, nullptr, SkCanvas::kStrict_SrcRectConstraint);
147     }
148 
149 private:
150     typedef skiagm::GM INHERITED;
151 };
152 
153 //////////////////////////////////////////////////////////////////////////////
make_big_bitmap(SkBitmap * bitmap)154 static void make_big_bitmap(SkBitmap* bitmap) {
155 
156     constexpr int gXSize = 4096;
157     constexpr int gYSize = 4096;
158     constexpr int gBorderWidth = 10;
159 
160     bitmap->allocN32Pixels(gXSize, gYSize);
161     for (int y = 0; y < gYSize; ++y) {
162         for (int x = 0; x < gXSize; ++x) {
163             if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
164                 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
165                 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
166             } else {
167                 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
168             }
169         }
170     }
171 }
172 
173 // This GM attempts to reveal any issues we may have when the GPU has to
174 // break up a large texture in order to draw it. The XOR transfer mode will
175 // create stripes in the image if there is imprecision in the destination
176 // tile placement.
177 class DrawBitmapRect4 : public skiagm::GM {
178     bool fUseIRect;
179     SkBitmap fBigBitmap;
180 
181 public:
DrawBitmapRect4(bool useIRect)182     DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
183         this->setBGColor(0x88444444);
184     }
185 
186 protected:
onShortName()187     SkString onShortName() override {
188         SkString str;
189         str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
190         return str;
191     }
192 
onISize()193     SkISize onISize() override {
194         return SkISize::Make(640, 480);
195     }
196 
onOnceBeforeDraw()197     void onOnceBeforeDraw() override {
198         make_big_bitmap(&fBigBitmap);
199     }
200 
onDraw(SkCanvas * canvas)201     void onDraw(SkCanvas* canvas) override {
202         SkPaint paint;
203         paint.setAlpha(128);
204         paint.setBlendMode(SkBlendMode::kXor);
205 
206         SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
207         SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
208 
209         SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
210         SkRect dstR2 = { 10, 410, 30, 430 };
211 
212         if (!fUseIRect) {
213             canvas->drawBitmapRect(fBigBitmap, srcR1, dstR1, &paint,
214                                    SkCanvas::kStrict_SrcRectConstraint);
215             canvas->drawBitmapRect(fBigBitmap, srcR2, dstR2, &paint,
216                                    SkCanvas::kStrict_SrcRectConstraint);
217         } else {
218             canvas->drawBitmapRect(fBigBitmap, srcR1.roundOut(), dstR1, &paint);
219             canvas->drawBitmapRect(fBigBitmap, srcR2.roundOut(), dstR2, &paint);
220         }
221     }
222 
223 private:
224     typedef skiagm::GM INHERITED;
225 };
226 
227 class BitmapRectRounding : public skiagm::GM {
228     SkBitmap fBM;
229 
230 public:
BitmapRectRounding()231     BitmapRectRounding() {}
232 
233 protected:
onShortName()234     SkString onShortName() override {
235         SkString str;
236         str.printf("bitmaprect_rounding");
237         return str;
238     }
239 
onISize()240     SkISize onISize() override {
241         return SkISize::Make(640, 480);
242     }
243 
onOnceBeforeDraw()244     void onOnceBeforeDraw() override {
245         fBM.allocN32Pixels(10, 10);
246         fBM.eraseColor(SK_ColorBLUE);
247     }
248 
249     // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
250     // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
251     // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
252     // the clip (in which case we should see all blue).
253     // The correct image should be all blue.
onDraw(SkCanvas * canvas)254     void onDraw(SkCanvas* canvas) override {
255         SkPaint paint;
256         paint.setColor(SK_ColorRED);
257 
258         const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
259         canvas->scale(0.9f, 0.9f);
260 
261         // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
262         canvas->drawRect(r, paint);
263         canvas->drawBitmapRect(fBM, r, nullptr);
264     }
265 
266 private:
267     typedef skiagm::GM INHERITED;
268 };
269 DEF_GM( return new BitmapRectRounding; )
270 
271 //////////////////////////////////////////////////////////////////////////////
272 
273 DEF_GM( return new DrawBitmapRect2(false); )
274 DEF_GM( return new DrawBitmapRect2(true); )
275 DEF_GM( return new DrawBitmapRect3(); )
276 
277 #ifndef SK_BUILD_FOR_ANDROID
278 DEF_GM( return new DrawBitmapRect4(false); )
279 DEF_GM( return new DrawBitmapRect4(true); )
280 #endif
281