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" 11 12// effects 13#include "SkGradientShader.h" 14 15 16namespace skiagm { 17 18static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) { 19 bm->setConfig(config, w, h); 20 bm->allocPixels(); 21 bm->eraseColor(0); 22 23 SkCanvas canvas(*bm); 24 25 SkScalar wScalar = SkIntToScalar(w); 26 SkScalar hScalar = SkIntToScalar(h); 27 28 SkPoint pt = { wScalar / 2, hScalar / 2 }; 29 30 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); 31 32 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, 33 SK_ColorGREEN, SK_ColorMAGENTA, 34 SK_ColorBLUE, SK_ColorCYAN, 35 SK_ColorRED}; 36 37 SkScalar pos[] = {0, 38 SK_Scalar1 / 6, 39 2 * SK_Scalar1 / 6, 40 3 * SK_Scalar1 / 6, 41 4 * SK_Scalar1 / 6, 42 5 * SK_Scalar1 / 6, 43 SK_Scalar1}; 44 45 SkPaint paint; 46 paint.setShader(SkGradientShader::CreateRadial( 47 pt, radius, 48 colors, pos, 49 SK_ARRAY_COUNT(colors), 50 SkShader::kRepeat_TileMode))->unref(); 51 SkRect rect = SkRect::MakeWH(wScalar, hScalar); 52 SkMatrix mat = SkMatrix::I(); 53 for (int i = 0; i < 4; ++i) { 54 paint.getShader()->setLocalMatrix(mat); 55 canvas.drawRect(rect, paint); 56 rect.inset(wScalar / 8, hScalar / 8); 57 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); 58 } 59} 60 61static const int gSize = 1024; 62 63class DrawBitmapRectGM : public GM { 64public: 65 DrawBitmapRectGM() { 66 } 67 68 SkBitmap fLargeBitmap; 69 70protected: 71 SkString onShortName() { 72 return SkString("drawbitmaprect"); 73 } 74 75 SkISize onISize() { return make_isize(gSize, gSize); } 76 77 virtual void onDraw(SkCanvas* canvas) { 78 static const int kBmpSize = 2048; 79 if (fLargeBitmap.isNull()) { 80 makebm(&fLargeBitmap, 81 SkBitmap::kARGB_8888_Config, 82 kBmpSize, kBmpSize); 83 } 84 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; 85 static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2); 86 87 static const int kPadX = 30; 88 static const int kPadY = 40; 89 SkPaint paint; 90 paint.setAlpha(0x20); 91 canvas->drawBitmapRect(fLargeBitmap, NULL, 92 SkRect::MakeWH(gSize * SK_Scalar1, 93 gSize * SK_Scalar1), 94 &paint); 95 canvas->translate(SK_Scalar1 * kPadX / 2, 96 SK_Scalar1 * kPadY / 2); 97 SkPaint blackPaint; 98 SkScalar titleHeight = SK_Scalar1 * 24; 99 blackPaint.setColor(SK_ColorBLACK); 100 blackPaint.setTextSize(titleHeight); 101 blackPaint.setAntiAlias(true); 102 SkString title; 103 title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize); 104 canvas->drawText(title.c_str(), title.size(), 0, 105 titleHeight, blackPaint); 106 107 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); 108 int rowCount = 0; 109 canvas->save(); 110 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { 111 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { 112 113 SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2, 114 (kBmpSize - h) / 2, 115 w, h); 116 canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect); 117 118 SkString label; 119 label.appendf("%d x %d", w, h); 120 blackPaint.setAntiAlias(true); 121 blackPaint.setStyle(SkPaint::kFill_Style); 122 blackPaint.setTextSize(SK_Scalar1 * 10); 123 SkScalar baseline = dstRect.height() + 124 blackPaint.getTextSize() + SK_Scalar1 * 3; 125 canvas->drawText(label.c_str(), label.size(), 126 0, baseline, 127 blackPaint); 128 blackPaint.setStyle(SkPaint::kStroke_Style); 129 blackPaint.setStrokeWidth(SK_Scalar1); 130 blackPaint.setAntiAlias(false); 131 canvas->drawRect(dstRect, blackPaint); 132 133 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); 134 ++rowCount; 135 if ((dstRect.width() + kPadX) * rowCount > gSize) { 136 canvas->restore(); 137 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); 138 canvas->save(); 139 rowCount = 0; 140 } 141 } 142 } 143 } 144 145private: 146 typedef GM INHERITED; 147}; 148 149////////////////////////////////////////////////////////////////////////////// 150 151static GM* MyFactory(void*) { return new DrawBitmapRectGM; } 152static GMRegistry reg(MyFactory); 153 154} 155 156