1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "gm.h" 9 #include "sk_tool_utils.h" 10 #include "SkBlurMask.h" 11 #include "SkBlurMaskFilter.h" 12 #include "SkColorPriv.h" 13 #include "SkGradientShader.h" 14 #include "SkImage.h" 15 #include "SkImage_Base.h" 16 #include "SkMathPriv.h" 17 #include "SkShader.h" 18 #include "SkSurface.h" 19 20 21 static SkBitmap make_chessbm(int w, int h) { 22 SkBitmap bm; 23 bm.allocN32Pixels(w, h); 24 25 for (int y = 0; y < bm.height(); y++) { 26 uint32_t* p = bm.getAddr32(0, y); 27 for (int x = 0; x < bm.width(); x++) { 28 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; 29 } 30 } 31 return bm; 32 } 33 34 // Creates a bitmap and a matching image. 35 static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) { 36 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); 37 38 auto surface(sk_tool_utils::makeSurface(origCanvas, info)); 39 SkCanvas* canvas = surface->getCanvas(); 40 41 canvas->clear(SK_ColorTRANSPARENT); 42 43 SkScalar wScalar = SkIntToScalar(w); 44 SkScalar hScalar = SkIntToScalar(h); 45 46 SkPoint pt = { wScalar / 2, hScalar / 2 }; 47 48 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); 49 50 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, 51 SK_ColorGREEN, SK_ColorMAGENTA, 52 SK_ColorBLUE, SK_ColorCYAN, 53 SK_ColorRED}; 54 55 SkScalar pos[] = {0, 56 SK_Scalar1 / 6, 57 2 * SK_Scalar1 / 6, 58 3 * SK_Scalar1 / 6, 59 4 * SK_Scalar1 / 6, 60 5 * SK_Scalar1 / 6, 61 SK_Scalar1}; 62 63 SkPaint paint; 64 SkRect rect = SkRect::MakeWH(wScalar, hScalar); 65 SkMatrix mat = SkMatrix::I(); 66 for (int i = 0; i < 4; ++i) { 67 paint.setShader(SkGradientShader::MakeRadial( 68 pt, radius, 69 colors, pos, 70 SK_ARRAY_COUNT(colors), 71 SkShader::kRepeat_TileMode, 72 0, &mat)); 73 canvas->drawRect(rect, paint); 74 rect.inset(wScalar / 8, hScalar / 8); 75 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); 76 } 77 78 auto image = surface->makeImageSnapshot(); 79 80 SkBitmap tempBM; 81 82 image->asLegacyBitmap(&tempBM); 83 84 // Let backends know we won't change this, so they don't have to deep copy it defensively. 85 tempBM.setImmutable(); 86 *resultBM = tempBM; 87 88 return image; 89 } 90 91 static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR, 92 const SkRect& dstR, const SkPaint* paint) { 93 canvas->drawBitmapRect(bm, srcR, dstR, paint); 94 } 95 96 static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR, 97 const SkRect& dstR, const SkPaint* paint) { 98 if (!bm.bounds().contains(srcR)) { 99 bitmapproc(canvas, nullptr, bm, srcR, dstR, paint); 100 return; 101 } 102 103 SkBitmap subset; 104 if (bm.extractSubset(&subset, srcR)) { 105 canvas->drawBitmapRect(subset, dstR, paint); 106 } 107 } 108 109 static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR, 110 const SkRect& dstR, const SkPaint* paint) { 111 canvas->drawImageRect(image, srcR, dstR, paint); 112 } 113 114 static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm, 115 const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) { 116 if (!image->bounds().contains(srcR)) { 117 imageproc(canvas, image, bm, srcR, dstR, paint); 118 return; 119 } 120 121 if (sk_sp<SkImage> subset = image->makeSubset(srcR)) { 122 canvas->drawImageRect(subset, dstR, paint); 123 } 124 } 125 126 typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&, 127 const SkPaint*); 128 129 constexpr int gSize = 1024; 130 constexpr int gBmpSize = 2048; 131 132 class DrawBitmapRectGM : public skiagm::GM { 133 public: 134 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) { 135 fName.set("drawbitmaprect"); 136 if (suffix) { 137 fName.append(suffix); 138 } 139 } 140 141 DrawRectRectProc* fProc; 142 SkBitmap fLargeBitmap; 143 sk_sp<SkImage> fImage; 144 SkString fName; 145 146 protected: 147 SkString onShortName() override { return fName; } 148 149 SkISize onISize() override { return SkISize::Make(gSize, gSize); } 150 151 void setupImage(SkCanvas* canvas) { 152 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize); 153 } 154 155 void onDraw(SkCanvas* canvas) override { 156 if (!fImage || !fImage->isValid(canvas->getGrContext())) { 157 this->setupImage(canvas); 158 } 159 160 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; 161 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); 162 163 const int kPadX = 30; 164 const int kPadY = 40; 165 SkPaint paint; 166 paint.setAlphaf(0.125f); 167 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint); 168 canvas->translate(SK_Scalar1 * kPadX / 2, 169 SK_Scalar1 * kPadY / 2); 170 SkPaint blackPaint; 171 SkScalar titleHeight = SK_Scalar1 * 24; 172 blackPaint.setColor(SK_ColorBLACK); 173 blackPaint.setAntiAlias(true); 174 175 SkFont font(sk_tool_utils::create_portable_typeface(), titleHeight); 176 177 SkString title; 178 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize); 179 canvas->drawString(title, 0, titleHeight, font, blackPaint); 180 181 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); 182 int rowCount = 0; 183 canvas->save(); 184 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { 185 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { 186 187 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h); 188 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr); 189 190 SkString label; 191 label.appendf("%d x %d", w, h); 192 blackPaint.setAntiAlias(true); 193 blackPaint.setStyle(SkPaint::kFill_Style); 194 font.setSize(SK_Scalar1 * 10); 195 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3; 196 canvas->drawString(label, 0, baseline, font, blackPaint); 197 blackPaint.setStyle(SkPaint::kStroke_Style); 198 blackPaint.setStrokeWidth(SK_Scalar1); 199 blackPaint.setAntiAlias(false); 200 canvas->drawRect(dstRect, blackPaint); 201 202 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); 203 ++rowCount; 204 if ((dstRect.width() + kPadX) * rowCount > gSize) { 205 canvas->restore(); 206 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); 207 canvas->save(); 208 rowCount = 0; 209 } 210 } 211 } 212 213 { 214 // test the following code path: 215 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() 216 SkIRect srcRect; 217 SkPaint paint; 218 SkBitmap bm; 219 220 bm = make_chessbm(5, 5); 221 paint.setFilterQuality(kLow_SkFilterQuality); 222 223 srcRect.setXYWH(1, 1, 3, 3); 224 paint.setMaskFilter(SkMaskFilter::MakeBlur( 225 kNormal_SkBlurStyle, 226 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)))); 227 228 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); 229 fProc(canvas, image.get(), bm, srcRect, dstRect, &paint); 230 } 231 } 232 233 private: 234 typedef skiagm::GM INHERITED; 235 }; 236 237 DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); ) 238 DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); ) 239 DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); ) 240 DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); ) 241