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 "SkBlurMask.h" 10 #include "SkBlurMaskFilter.h" 11 #include "SkColorPriv.h" 12 #include "SkGradientShader.h" 13 #include "SkShader.h" 14 15 namespace skiagm { 16 17 static SkBitmap make_chessbm(int w, int h) { 18 SkBitmap bm; 19 bm.allocN32Pixels(w, h); 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 31 static void makebm(SkBitmap* bm, int w, int h) { 32 bm->allocN32Pixels(w, h); 33 bm->eraseColor(SK_ColorTRANSPARENT); 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 SkRect rect = SkRect::MakeWH(wScalar, hScalar); 59 SkMatrix mat = SkMatrix::I(); 60 for (int i = 0; i < 4; ++i) { 61 paint.setShader(SkGradientShader::CreateRadial( 62 pt, radius, 63 colors, pos, 64 SK_ARRAY_COUNT(colors), 65 SkShader::kRepeat_TileMode, 66 0, &mat))->unref(); 67 canvas.drawRect(rect, paint); 68 rect.inset(wScalar / 8, hScalar / 8); 69 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); 70 } 71 // Let backends know we won't change this, so they don't have to deep copy it defensively. 72 bm->setImmutable(); 73 } 74 75 static const int gSize = 1024; 76 77 class DrawBitmapRectGM : public GM { 78 public: 79 DrawBitmapRectGM() { 80 } 81 82 SkBitmap fLargeBitmap; 83 84 protected: 85 SkString onShortName() { 86 return SkString("drawbitmaprect"); 87 } 88 89 SkISize onISize() { return SkISize::Make(gSize, gSize); } 90 91 virtual void onDraw(SkCanvas* canvas) { 92 static const int kBmpSize = 2048; 93 if (fLargeBitmap.isNull()) { 94 makebm(&fLargeBitmap, kBmpSize, kBmpSize); 95 } 96 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; 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 sk_tool_utils::set_portable_typeface(&blackPaint); 115 SkString title; 116 title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize); 117 canvas->drawText(title.c_str(), title.size(), 0, 118 titleHeight, blackPaint); 119 120 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); 121 int rowCount = 0; 122 canvas->save(); 123 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { 124 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { 125 126 SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2, 127 (kBmpSize - h) / 2, 128 w, h); 129 canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect); 130 131 SkString label; 132 label.appendf("%d x %d", w, h); 133 blackPaint.setAntiAlias(true); 134 blackPaint.setStyle(SkPaint::kFill_Style); 135 blackPaint.setTextSize(SK_Scalar1 * 10); 136 SkScalar baseline = dstRect.height() + 137 blackPaint.getTextSize() + SK_Scalar1 * 3; 138 canvas->drawText(label.c_str(), label.size(), 139 0, baseline, 140 blackPaint); 141 blackPaint.setStyle(SkPaint::kStroke_Style); 142 blackPaint.setStrokeWidth(SK_Scalar1); 143 blackPaint.setAntiAlias(false); 144 canvas->drawRect(dstRect, blackPaint); 145 146 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); 147 ++rowCount; 148 if ((dstRect.width() + kPadX) * rowCount > gSize) { 149 canvas->restore(); 150 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); 151 canvas->save(); 152 rowCount = 0; 153 } 154 } 155 } 156 157 { 158 // test the following code path: 159 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() 160 SkIRect srcRect; 161 SkPaint paint; 162 SkBitmap bm; 163 164 bm = make_chessbm(5, 5); 165 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 166 167 srcRect.setXYWH(1, 1, 3, 3); 168 SkMaskFilter* mf = SkBlurMaskFilter::Create( 169 kNormal_SkBlurStyle, 170 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), 171 SkBlurMaskFilter::kHighQuality_BlurFlag | 172 SkBlurMaskFilter::kIgnoreTransform_BlurFlag); 173 paint.setMaskFilter(mf)->unref(); 174 canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint); 175 } 176 } 177 178 private: 179 typedef GM INHERITED; 180 }; 181 182 ////////////////////////////////////////////////////////////////////////////// 183 184 static GM* MyFactory(void*) { return new DrawBitmapRectGM; } 185 static GMRegistry reg(MyFactory); 186 } 187