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 10 #include "Resources.h" 11 #include "SkGradientShader.h" 12 #include "SkImageDecoder.h" 13 #include "SkStream.h" 14 #include "SkTypeface.h" 15 16 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { 17 sk_tool_utils::set_portable_typeface(paint, name, style); 18 } 19 20 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { 21 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), 22 SkIntToScalar(bm.height())); 23 mat.mapRect(&bounds); 24 return SkSize::Make(bounds.width(), bounds.height()); 25 } 26 27 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) { 28 SkPaint paint; 29 30 SkAutoCanvasRestore acr(canvas, true); 31 32 canvas->drawBitmapMatrix(bm, mat, &paint); 33 34 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 35 canvas->translate(dx, 0); 36 canvas->drawBitmapMatrix(bm, mat, &paint); 37 38 paint.setFilterLevel(SkPaint::kMedium_FilterLevel); 39 canvas->translate(dx, 0); 40 canvas->drawBitmapMatrix(bm, mat, &paint); 41 42 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); 43 canvas->translate(dx, 0); 44 canvas->drawBitmapMatrix(bm, mat, &paint); 45 } 46 47 class FilterBitmapGM : public skiagm::GM { 48 void onOnceBeforeDraw() { 49 50 this->makeBitmap(); 51 52 SkScalar cx = SkScalarHalf(fBM.width()); 53 SkScalar cy = SkScalarHalf(fBM.height()); 54 SkScalar scale = this->getScale(); 55 56 // these two matrices use a scale factor configured by the subclass 57 fMatrix[0].setScale(scale, scale); 58 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); 59 60 // up/down scaling mix 61 fMatrix[2].setScale(0.7f, 1.05f); 62 } 63 64 public: 65 SkBitmap fBM; 66 SkMatrix fMatrix[3]; 67 SkString fName; 68 69 FilterBitmapGM() 70 { 71 this->setBGColor(0xFFDDDDDD); 72 } 73 74 protected: 75 virtual uint32_t onGetFlags() const SK_OVERRIDE { 76 return kSkipTiled_Flag; 77 } 78 79 virtual SkString onShortName() SK_OVERRIDE { 80 return fName; 81 } 82 83 virtual SkISize onISize() SK_OVERRIDE { 84 return SkISize::Make(1024, 768); 85 } 86 87 virtual void makeBitmap() = 0; 88 virtual SkScalar getScale() = 0; 89 90 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 91 92 canvas->translate(10, 10); 93 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { 94 SkSize size = computeSize(fBM, fMatrix[i]); 95 size.fWidth += 20; 96 size.fHeight += 20; 97 98 draw_row(canvas, fBM, fMatrix[i], size.fWidth); 99 canvas->translate(0, size.fHeight); 100 } 101 } 102 103 private: 104 typedef skiagm::GM INHERITED; 105 }; 106 107 class FilterBitmapTextGM: public FilterBitmapGM { 108 public: 109 FilterBitmapTextGM(float textSize) 110 : fTextSize(textSize) 111 { 112 fName.printf("filterbitmap_text_%.2fpt", fTextSize); 113 } 114 115 protected: 116 float fTextSize; 117 118 SkScalar getScale() SK_OVERRIDE { 119 return 32.f/fTextSize; 120 } 121 122 void makeBitmap() SK_OVERRIDE { 123 fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); 124 SkCanvas canvas(fBM); 125 canvas.drawColor(SK_ColorWHITE); 126 127 SkPaint paint; 128 paint.setAntiAlias(true); 129 paint.setSubpixelText(true); 130 paint.setTextSize(fTextSize); 131 132 setTypeface(&paint, "Times", SkTypeface::kNormal); 133 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); 134 setTypeface(&paint, "Times", SkTypeface::kBold); 135 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); 136 setTypeface(&paint, "Times", SkTypeface::kItalic); 137 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); 138 setTypeface(&paint, "Times", SkTypeface::kBoldItalic); 139 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); 140 } 141 private: 142 typedef FilterBitmapGM INHERITED; 143 }; 144 145 class FilterBitmapCheckerboardGM: public FilterBitmapGM { 146 public: 147 FilterBitmapCheckerboardGM(int size, int num_checks) 148 : fSize(size), fNumChecks(num_checks) 149 { 150 fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); 151 } 152 153 protected: 154 int fSize; 155 int fNumChecks; 156 157 SkScalar getScale() SK_OVERRIDE { 158 return 192.f/fSize; 159 } 160 161 void makeBitmap() SK_OVERRIDE { 162 fBM.allocN32Pixels(fSize, fSize); 163 for (int y = 0; y < fSize; y ++) { 164 for (int x = 0; x < fSize; x ++) { 165 SkPMColor* s = fBM.getAddr32(x, y); 166 int cx = (x * fNumChecks) / fSize; 167 int cy = (y * fNumChecks) / fSize; 168 if ((cx+cy)%2) { 169 *s = 0xFFFFFFFF; 170 } else { 171 *s = 0xFF000000; 172 } 173 } 174 } 175 } 176 private: 177 typedef FilterBitmapGM INHERITED; 178 }; 179 180 class FilterBitmapImageGM: public FilterBitmapGM { 181 public: 182 FilterBitmapImageGM(const char filename[]) 183 : fFilename(filename) 184 { 185 fName.printf("filterbitmap_image_%s", filename); 186 } 187 188 protected: 189 SkString fFilename; 190 int fSize; 191 192 SkScalar getScale() SK_OVERRIDE { 193 return 192.f/fSize; 194 } 195 196 void makeBitmap() SK_OVERRIDE { 197 SkImageDecoder* codec = NULL; 198 SkString resourcePath = GetResourcePath(fFilename.c_str()); 199 SkFILEStream stream(resourcePath.c_str()); 200 if (stream.isValid()) { 201 codec = SkImageDecoder::Factory(&stream); 202 } 203 if (codec) { 204 stream.rewind(); 205 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); 206 SkDELETE(codec); 207 } else { 208 fBM.allocN32Pixels(1, 1); 209 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 210 } 211 fSize = fBM.height(); 212 } 213 private: 214 typedef FilterBitmapGM INHERITED; 215 }; 216 217 ////////////////////////////////////////////////////////////////////////////// 218 219 DEF_GM( return new FilterBitmapTextGM(3); ) 220 DEF_GM( return new FilterBitmapTextGM(7); ) 221 DEF_GM( return new FilterBitmapTextGM(10); ) 222 DEF_GM( return new FilterBitmapCheckerboardGM(4,4); ) 223 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) 224 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) 225 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) 226 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) 227 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) 228 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) 229 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) 230 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) 231 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) 232 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) 233