3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkGradientShader.h"
11 #include "SkGraphics.h"
16 static void make_bitmap(SkBitmap* bitmap) {
17 bitmap->allocN32Pixels(64, 64);
19 SkCanvas canvas(*bitmap);
21 canvas.drawColor(SK_ColorRED);
23 paint.setAntiAlias(true);
24 const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
25 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
26 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
27 SkShader::kClamp_TileMode))->unref();
28 canvas.drawCircle(32, 32, 32, paint);
31 class DrawBitmapRect2 : public skiagm::GM {
34 DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
38 SkString onShortName() SK_OVERRIDE {
40 str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
44 SkISize onISize() SK_OVERRIDE {
45 return SkISize::Make(640, 480);
48 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
49 canvas->drawColor(0xFFCCCCCC);
51 const SkIRect src[] = {
59 paint.setStyle(SkPaint::kStroke_Style);
64 SkRect dstR = { 0, 200, 128, 380 };
66 canvas->translate(16, 40);
67 for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
71 canvas->drawBitmap(bitmap, 0, 0, &paint);
73 canvas->drawBitmapRectToRect(bitmap, &srcR, dstR, &paint);
75 canvas->drawBitmapRect(bitmap, &src[i], dstR, &paint);
78 canvas->drawRect(dstR, paint);
79 canvas->drawRect(srcR, paint);
81 canvas->translate(160, 0);
86 typedef skiagm::GM INHERITED;
89 //////////////////////////////////////////////////////////////////////////////
91 static void make_3x3_bitmap(SkBitmap* bitmap) {
95 const SkColor textureData[xSize][ySize] = {
96 { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
97 { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
98 { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
101 bitmap->allocN32Pixels(xSize, ySize, true);
102 SkCanvas canvas(*bitmap);
105 for (int y = 0; y < ySize; y++) {
106 for (int x = 0; x < xSize; x++) {
107 paint.setColor(textureData[x][y]);
108 canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
113 // This GM attempts to make visible any issues drawBitmapRectToRect may have
114 // with partial source rects. In this case the eight pixels on the border
115 // should be half the width/height of the central pixel, i.e.:
120 class DrawBitmapRect3 : public skiagm::GM {
123 this->setBGColor(SK_ColorBLACK);
127 SkString onShortName() SK_OVERRIDE {
129 str.printf("3x3bitmaprect");
133 SkISize onISize() SK_OVERRIDE {
134 return SkISize::Make(640, 480);
137 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
140 make_3x3_bitmap(&bitmap);
142 SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
143 SkRect dstR = { 100, 100, 300, 200 };
145 canvas->drawBitmapRectToRect(bitmap, &srcR, dstR, NULL);
149 typedef skiagm::GM INHERITED;
152 //////////////////////////////////////////////////////////////////////////////
153 static void make_big_bitmap(SkBitmap* bitmap) {
155 static const int gXSize = 4096;
156 static const int gYSize = 4096;
157 static const int gBorderWidth = 10;
159 bitmap->allocN32Pixels(gXSize, gYSize);
160 for (int y = 0; y < gYSize; ++y) {
161 for (int x = 0; x < gXSize; ++x) {
162 if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
163 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
164 *bitmap->getAddr32(x, y) = 0x88FFFFFF;
166 *bitmap->getAddr32(x, y) = 0x88FF0000;
172 // This GM attempts to reveal any issues we may have when the GPU has to
173 // break up a large texture in order to draw it. The XOR transfer mode will
174 // create stripes in the image if there is imprecision in the destination
176 class DrawBitmapRect4 : public skiagm::GM {
181 DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
182 this->setBGColor(0x88444444);
186 SkString onShortName() SK_OVERRIDE {
188 str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
192 SkISize onISize() SK_OVERRIDE {
193 return SkISize::Make(640, 480);
196 void onOnceBeforeDraw() SK_OVERRIDE {
197 make_big_bitmap(&fBigBitmap);
200 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
202 SkXfermode* mode = SkXfermode::Create(SkXfermode::kXor_Mode);
206 paint.setXfermode(mode)->unref();
208 SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
209 SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
211 SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
212 SkRect dstR2 = { 10, 410, 30, 430 };
215 canvas->drawBitmapRectToRect(fBigBitmap, &srcR1, dstR1, &paint);
216 canvas->drawBitmapRectToRect(fBigBitmap, &srcR2, dstR2, &paint);
218 SkIRect iSrcR1, iSrcR2;
220 srcR1.roundOut(&iSrcR1);
221 srcR2.roundOut(&iSrcR2);
223 canvas->drawBitmapRect(fBigBitmap, &iSrcR1, dstR1, &paint);
224 canvas->drawBitmapRect(fBigBitmap, &iSrcR2, dstR2, &paint);
229 typedef skiagm::GM INHERITED;
232 class BitmapRectRounding : public skiagm::GM {
236 BitmapRectRounding() {}
239 SkString onShortName() SK_OVERRIDE {
241 str.printf("bitmaprect_rounding");
245 SkISize onISize() SK_OVERRIDE {
246 return SkISize::Make(640, 480);
249 void onOnceBeforeDraw() SK_OVERRIDE {
250 fBM.allocN32Pixels(10, 10);
251 fBM.eraseColor(SK_ColorBLUE);
254 // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
255 // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
256 // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
257 // the clip (in which case we should see all blue).
258 // The correct image should be all blue.
259 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
261 paint.setColor(SK_ColorRED);
263 const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
264 canvas->scale(0.9f, 0.9f);
266 // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
267 canvas->drawRect(r, paint);
268 canvas->drawBitmapRect(fBM, NULL, r, NULL);
272 typedef skiagm::GM INHERITED;
274 DEF_GM( return new BitmapRectRounding; )
276 //////////////////////////////////////////////////////////////////////////////
278 static skiagm::GM* MyFactory0(void*) { return new DrawBitmapRect2(false); }
279 static skiagm::GM* MyFactory1(void*) { return new DrawBitmapRect2(true); }
281 static skiagm::GM* MyFactory2(void*) { return new DrawBitmapRect3(); }
283 #ifndef SK_BUILD_FOR_ANDROID
284 static skiagm::GM* MyFactory3(void*) { return new DrawBitmapRect4(false); }
285 static skiagm::GM* MyFactory4(void*) { return new DrawBitmapRect4(true); }
288 static skiagm::GMRegistry reg0(MyFactory0);
289 static skiagm::GMRegistry reg1(MyFactory1);
291 static skiagm::GMRegistry reg2(MyFactory2);
293 #ifndef SK_BUILD_FOR_ANDROID
294 static skiagm::GMRegistry reg3(MyFactory3);
295 static skiagm::GMRegistry reg4(MyFactory4);