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