reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1 | |
| 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 | */ |
| 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 | |
| 16 | static void make_bitmap(SkBitmap* bitmap) { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 17 | bitmap->allocN32Pixels(64, 64); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 18 | |
mike@reedtribe.org | 3bd2173 | 2012-09-26 02:45:10 +0000 | [diff] [blame] | 19 | SkCanvas canvas(*bitmap); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 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::CreateLinear(pts, colors, NULL, 2, |
| 27 | SkShader::kClamp_TileMode))->unref(); |
| 28 | canvas.drawCircle(32, 32, 32, paint); |
| 29 | } |
| 30 | |
| 31 | class DrawBitmapRect2 : public skiagm::GM { |
| 32 | bool fUseIRect; |
| 33 | public: |
| 34 | DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) { |
| 35 | } |
| 36 | |
| 37 | protected: |
| 38 | virtual SkString onShortName() SK_OVERRIDE { |
| 39 | SkString str; |
| 40 | str.printf("bitmaprect_%s", fUseIRect ? "i" : "s"); |
| 41 | return str; |
| 42 | } |
| 43 | |
| 44 | virtual SkISize onISize() SK_OVERRIDE { |
| 45 | return SkISize::Make(640, 480); |
| 46 | } |
| 47 | |
| 48 | virtual void onDraw(SkCanvas* canvas) SK_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); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 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); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 72 | if (!fUseIRect) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 73 | canvas->drawBitmapRectToRect(bitmap, &srcR, dstR, &paint); |
| 74 | } else { |
| 75 | canvas->drawBitmapRect(bitmap, &src[i], dstR, &paint); |
| 76 | } |
| 77 | |
| 78 | canvas->drawRect(dstR, paint); |
| 79 | canvas->drawRect(srcR, paint); |
| 80 | |
| 81 | canvas->translate(160, 0); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | typedef skiagm::GM INHERITED; |
| 87 | }; |
| 88 | |
| 89 | ////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 90 | static void make_3x3_bitmap(SkBitmap* bitmap) { |
| 91 | |
| 92 | static const int gXSize = 3; |
| 93 | static const int gYSize = 3; |
| 94 | |
| 95 | SkColor textureData[gXSize][gYSize] = { |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 96 | { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE }, |
| 97 | { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN }, |
| 98 | { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA } |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 102 | bitmap->allocN32Pixels(gXSize, gYSize); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 103 | for (int y = 0; y < gYSize; y++) { |
| 104 | for (int x = 0; x < gXSize; x++) { |
| 105 | *bitmap->getAddr32(x, y) = textureData[x][y]; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // This GM attempts to make visible any issues drawBitmapRectToRect may have |
| 111 | // with partial source rects. In this case the eight pixels on the border |
| 112 | // should be half the width/height of the central pixel, i.e.: |
| 113 | // __|____|__ |
| 114 | // | | |
| 115 | // __|____|__ |
| 116 | // | | |
| 117 | class DrawBitmapRect3 : public skiagm::GM { |
| 118 | public: |
| 119 | DrawBitmapRect3() { |
| 120 | this->setBGColor(SK_ColorBLACK); |
| 121 | } |
| 122 | |
| 123 | protected: |
| 124 | virtual SkString onShortName() SK_OVERRIDE { |
| 125 | SkString str; |
| 126 | str.printf("3x3bitmaprect"); |
| 127 | return str; |
| 128 | } |
| 129 | |
| 130 | virtual SkISize onISize() SK_OVERRIDE { |
| 131 | return SkISize::Make(640, 480); |
| 132 | } |
| 133 | |
| 134 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 135 | |
| 136 | SkBitmap bitmap; |
| 137 | make_3x3_bitmap(&bitmap); |
| 138 | |
| 139 | SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f }; |
| 140 | SkRect dstR = { 100, 100, 300, 200 }; |
| 141 | |
| 142 | canvas->drawBitmapRectToRect(bitmap, &srcR, dstR, NULL); |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | typedef skiagm::GM INHERITED; |
| 147 | }; |
| 148 | |
| 149 | ////////////////////////////////////////////////////////////////////////////// |
| 150 | static void make_big_bitmap(SkBitmap* bitmap) { |
| 151 | |
| 152 | static const int gXSize = 4096; |
| 153 | static const int gYSize = 4096; |
| 154 | static const int gBorderWidth = 10; |
| 155 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 156 | bitmap->allocN32Pixels(gXSize, gYSize); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 157 | for (int y = 0; y < gYSize; ++y) { |
| 158 | for (int x = 0; x < gXSize; ++x) { |
skia.committer@gmail.com | 44d4988 | 2012-09-27 02:01:04 +0000 | [diff] [blame] | 159 | if (x <= gBorderWidth || x >= gXSize-gBorderWidth || |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 160 | y <= gBorderWidth || y >= gYSize-gBorderWidth) { |
| 161 | *bitmap->getAddr32(x, y) = 0x88FFFFFF; |
| 162 | } else { |
| 163 | *bitmap->getAddr32(x, y) = 0x88FF0000; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // This GM attempts to reveal any issues we may have when the GPU has to |
| 170 | // break up a large texture in order to draw it. The XOR transfer mode will |
| 171 | // create stripes in the image if there is imprecision in the destination |
| 172 | // tile placement. |
| 173 | class DrawBitmapRect4 : public skiagm::GM { |
| 174 | bool fUseIRect; |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 175 | SkBitmap fBigBitmap; |
| 176 | |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 177 | public: |
| 178 | DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) { |
| 179 | this->setBGColor(0x88444444); |
| 180 | } |
| 181 | |
| 182 | protected: |
| 183 | virtual SkString onShortName() SK_OVERRIDE { |
| 184 | SkString str; |
| 185 | str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s"); |
| 186 | return str; |
| 187 | } |
| 188 | |
| 189 | virtual SkISize onISize() SK_OVERRIDE { |
| 190 | return SkISize::Make(640, 480); |
| 191 | } |
| 192 | |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 193 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 194 | make_big_bitmap(&fBigBitmap); |
| 195 | } |
| 196 | |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 197 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 198 | |
| 199 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kXor_Mode); |
| 200 | |
| 201 | SkPaint paint; |
| 202 | paint.setAlpha(128); |
robertphillips@google.com | 8fdb4c1 | 2012-10-02 11:47:16 +0000 | [diff] [blame] | 203 | paint.setXfermode(mode)->unref(); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 204 | |
robertphillips@google.com | ffad46b | 2012-10-01 14:32:51 +0000 | [diff] [blame] | 205 | SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f }; |
| 206 | SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f }; |
| 207 | |
| 208 | SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f }; |
| 209 | SkRect dstR2 = { 10, 410, 30, 430 }; |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 210 | |
| 211 | if (!fUseIRect) { |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 212 | canvas->drawBitmapRectToRect(fBigBitmap, &srcR1, dstR1, &paint); |
| 213 | canvas->drawBitmapRectToRect(fBigBitmap, &srcR2, dstR2, &paint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 214 | } else { |
robertphillips@google.com | ffad46b | 2012-10-01 14:32:51 +0000 | [diff] [blame] | 215 | SkIRect iSrcR1, iSrcR2; |
| 216 | |
| 217 | srcR1.roundOut(&iSrcR1); |
| 218 | srcR2.roundOut(&iSrcR2); |
| 219 | |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 220 | canvas->drawBitmapRect(fBigBitmap, &iSrcR1, dstR1, &paint); |
| 221 | canvas->drawBitmapRect(fBigBitmap, &iSrcR2, dstR2, &paint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | typedef skiagm::GM INHERITED; |
| 227 | }; |
| 228 | |
| 229 | ////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 230 | |
| 231 | static skiagm::GM* MyFactory0(void*) { return new DrawBitmapRect2(false); } |
| 232 | static skiagm::GM* MyFactory1(void*) { return new DrawBitmapRect2(true); } |
| 233 | |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 234 | static skiagm::GM* MyFactory2(void*) { return new DrawBitmapRect3(); } |
| 235 | |
robertphillips@google.com | 653b0d6 | 2012-09-26 15:28:04 +0000 | [diff] [blame] | 236 | #ifndef SK_BUILD_FOR_ANDROID |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 237 | static skiagm::GM* MyFactory3(void*) { return new DrawBitmapRect4(false); } |
| 238 | static skiagm::GM* MyFactory4(void*) { return new DrawBitmapRect4(true); } |
robertphillips@google.com | 653b0d6 | 2012-09-26 15:28:04 +0000 | [diff] [blame] | 239 | #endif |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 240 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 241 | static skiagm::GMRegistry reg0(MyFactory0); |
| 242 | static skiagm::GMRegistry reg1(MyFactory1); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 243 | |
| 244 | static skiagm::GMRegistry reg2(MyFactory2); |
| 245 | |
robertphillips@google.com | 653b0d6 | 2012-09-26 15:28:04 +0000 | [diff] [blame] | 246 | #ifndef SK_BUILD_FOR_ANDROID |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 247 | static skiagm::GMRegistry reg3(MyFactory3); |
| 248 | static skiagm::GMRegistry reg4(MyFactory4); |
robertphillips@google.com | 653b0d6 | 2012-09-26 15:28:04 +0000 | [diff] [blame] | 249 | #endif |