bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +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" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 17 | static SkBitmap make_chessbm(int w, int h) { |
| 18 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame^] | 19 | bm.allocN32Pixels(w, h); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 20 | |
| 21 | for (int y = 0; y < bm.height(); y++) { |
| 22 | uint32_t* p = bm.getAddr32(0, y); |
| 23 | for (int x = 0; x < bm.width(); x++) { |
| 24 | p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
| 25 | } |
| 26 | } |
| 27 | bm.unlockPixels(); |
| 28 | return bm; |
| 29 | } |
| 30 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 31 | static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame^] | 32 | bm->allocConfigPixels(config, w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 33 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 34 | |
| 35 | SkCanvas canvas(*bm); |
| 36 | |
| 37 | SkScalar wScalar = SkIntToScalar(w); |
| 38 | SkScalar hScalar = SkIntToScalar(h); |
| 39 | |
| 40 | SkPoint pt = { wScalar / 2, hScalar / 2 }; |
| 41 | |
| 42 | SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); |
| 43 | |
| 44 | SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, |
| 45 | SK_ColorGREEN, SK_ColorMAGENTA, |
| 46 | SK_ColorBLUE, SK_ColorCYAN, |
| 47 | SK_ColorRED}; |
| 48 | |
| 49 | SkScalar pos[] = {0, |
| 50 | SK_Scalar1 / 6, |
| 51 | 2 * SK_Scalar1 / 6, |
| 52 | 3 * SK_Scalar1 / 6, |
| 53 | 4 * SK_Scalar1 / 6, |
| 54 | 5 * SK_Scalar1 / 6, |
| 55 | SK_Scalar1}; |
| 56 | |
| 57 | SkPaint paint; |
| 58 | paint.setShader(SkGradientShader::CreateRadial( |
| 59 | pt, radius, |
| 60 | colors, pos, |
| 61 | SK_ARRAY_COUNT(colors), |
| 62 | SkShader::kRepeat_TileMode))->unref(); |
| 63 | SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
| 64 | SkMatrix mat = SkMatrix::I(); |
| 65 | for (int i = 0; i < 4; ++i) { |
| 66 | paint.getShader()->setLocalMatrix(mat); |
| 67 | canvas.drawRect(rect, paint); |
| 68 | rect.inset(wScalar / 8, hScalar / 8); |
| 69 | mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static const int gSize = 1024; |
| 74 | |
| 75 | class DrawBitmapRectGM : public GM { |
| 76 | public: |
| 77 | DrawBitmapRectGM() { |
| 78 | } |
| 79 | |
| 80 | SkBitmap fLargeBitmap; |
| 81 | |
| 82 | protected: |
| 83 | SkString onShortName() { |
| 84 | return SkString("drawbitmaprect"); |
| 85 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 87 | SkISize onISize() { return make_isize(gSize, gSize); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 89 | virtual void onDraw(SkCanvas* canvas) { |
| 90 | static const int kBmpSize = 2048; |
| 91 | if (fLargeBitmap.isNull()) { |
| 92 | makebm(&fLargeBitmap, |
| 93 | SkBitmap::kARGB_8888_Config, |
| 94 | kBmpSize, kBmpSize); |
| 95 | } |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 96 | SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 97 | static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2); |
| 98 | |
| 99 | static const int kPadX = 30; |
| 100 | static const int kPadY = 40; |
| 101 | SkPaint paint; |
| 102 | paint.setAlpha(0x20); |
| 103 | canvas->drawBitmapRect(fLargeBitmap, NULL, |
| 104 | SkRect::MakeWH(gSize * SK_Scalar1, |
| 105 | gSize * SK_Scalar1), |
| 106 | &paint); |
| 107 | canvas->translate(SK_Scalar1 * kPadX / 2, |
| 108 | SK_Scalar1 * kPadY / 2); |
| 109 | SkPaint blackPaint; |
| 110 | SkScalar titleHeight = SK_Scalar1 * 24; |
| 111 | blackPaint.setColor(SK_ColorBLACK); |
| 112 | blackPaint.setTextSize(titleHeight); |
| 113 | blackPaint.setAntiAlias(true); |
| 114 | SkString title; |
| 115 | title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize); |
| 116 | canvas->drawText(title.c_str(), title.size(), 0, |
| 117 | titleHeight, blackPaint); |
| 118 | |
| 119 | canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); |
| 120 | int rowCount = 0; |
| 121 | canvas->save(); |
| 122 | for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { |
| 123 | for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { |
| 124 | |
| 125 | SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2, |
| 126 | (kBmpSize - h) / 2, |
| 127 | w, h); |
| 128 | canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect); |
| 129 | |
| 130 | SkString label; |
| 131 | label.appendf("%d x %d", w, h); |
| 132 | blackPaint.setAntiAlias(true); |
| 133 | blackPaint.setStyle(SkPaint::kFill_Style); |
| 134 | blackPaint.setTextSize(SK_Scalar1 * 10); |
| 135 | SkScalar baseline = dstRect.height() + |
| 136 | blackPaint.getTextSize() + SK_Scalar1 * 3; |
| 137 | canvas->drawText(label.c_str(), label.size(), |
| 138 | 0, baseline, |
| 139 | blackPaint); |
| 140 | blackPaint.setStyle(SkPaint::kStroke_Style); |
| 141 | blackPaint.setStrokeWidth(SK_Scalar1); |
| 142 | blackPaint.setAntiAlias(false); |
| 143 | canvas->drawRect(dstRect, blackPaint); |
| 144 | |
| 145 | canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); |
| 146 | ++rowCount; |
| 147 | if ((dstRect.width() + kPadX) * rowCount > gSize) { |
| 148 | canvas->restore(); |
| 149 | canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); |
| 150 | canvas->save(); |
| 151 | rowCount = 0; |
| 152 | } |
| 153 | } |
| 154 | } |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 155 | |
| 156 | { |
| 157 | // test the following code path: |
| 158 | // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() |
| 159 | SkIRect srcRect; |
| 160 | SkPaint paint; |
| 161 | SkBitmap bm; |
| 162 | |
| 163 | bm = make_chessbm(5, 5); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 164 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 165 | |
| 166 | srcRect.setXYWH(1, 1, 3, 3); |
| 167 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 168 | SkBlurMaskFilter::kNormal_BlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 169 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 170 | SkBlurMaskFilter::kHighQuality_BlurFlag | |
| 171 | SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
| 172 | paint.setMaskFilter(mf)->unref(); |
| 173 | canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint); |
| 174 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
| 178 | typedef GM INHERITED; |
| 179 | }; |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
borenet@google.com | ceb2a03 | 2012-06-25 21:03:04 +0000 | [diff] [blame] | 183 | #ifndef SK_BUILD_FOR_ANDROID |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 184 | static GM* MyFactory(void*) { return new DrawBitmapRectGM; } |
| 185 | static GMRegistry reg(MyFactory); |
borenet@google.com | ceb2a03 | 2012-06-25 21:03:04 +0000 | [diff] [blame] | 186 | #endif |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 187 | } |