BitmapRectBench.cpp revision 6485b0be74f66587b51ca3c476b24b0e2674ca5b
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 "SkBenchmark.h" 9#include "SkBitmap.h" 10#include "SkPaint.h" 11#include "SkCanvas.h" 12#include "SkColorPriv.h" 13#include "SkRandom.h" 14#include "SkString.h" 15 16static void draw_into_bitmap(const SkBitmap& bm) { 17 const int w = bm.width(); 18 const int h = bm.height(); 19 20 SkCanvas canvas(bm); 21 SkPaint p; 22 p.setAntiAlias(true); 23 p.setColor(SK_ColorRED); 24 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, 25 SkIntToScalar(SkMin32(w, h))*3/8, p); 26 27 SkRect r; 28 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); 29 p.setStyle(SkPaint::kStroke_Style); 30 p.setStrokeWidth(SkIntToScalar(4)); 31 p.setColor(SK_ColorBLUE); 32 canvas.drawRect(r, p); 33} 34 35/* Variants for bitmaprect 36 src : entire bitmap, subset, fractional subset 37 dst : same size as src, diff size 38 paint : filter-p 39 */ 40 41class BitmapRectBench : public SkBenchmark { 42 SkBitmap fBitmap; 43 bool fDoFilter; 44 bool fSlightMatrix; 45 uint8_t fAlpha; 46 SkString fName; 47 SkRect fSrcR, fDstR; 48 static const int kWidth = 128; 49 static const int kHeight = 128; 50 enum { N = SkBENCHLOOP(300) }; 51public: 52 BitmapRectBench(void* param, U8CPU alpha, bool doFilter, bool slightMatrix) : INHERITED(param) { 53 fAlpha = SkToU8(alpha); 54 fDoFilter = doFilter; 55 fSlightMatrix = slightMatrix; 56 57 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight); 58 } 59 60protected: 61 virtual const char* onGetName() SK_OVERRIDE { 62 fName.printf("bitmaprect_%02X_%sfilter_%s", 63 fAlpha, fDoFilter ? "" : "no", 64 fSlightMatrix ? "trans" : "identity"); 65 return fName.c_str(); 66 } 67 68 virtual void onPreDraw() SK_OVERRIDE { 69 fBitmap.allocPixels(); 70 fBitmap.setIsOpaque(true); 71 fBitmap.eraseColor(SK_ColorBLACK); 72 draw_into_bitmap(fBitmap); 73 74 fSrcR.iset(0, 0, kWidth, kHeight); 75 fDstR.iset(0, 0, kWidth, kHeight); 76 77 if (fSlightMatrix) { 78 // want fractional translate 79 fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7); 80 // want enough to create a scale matrix, but not enough to scare 81 // off our sniffer which tries to see if the matrix is "effectively" 82 // translate-only. 83 fDstR.fRight += SK_Scalar1 / (kWidth * 60); 84 } 85 } 86 87 88 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 89 SkMWCRandom rand; 90 91 SkPaint paint; 92 this->setupPaint(&paint); 93 paint.setFilterBitmap(fDoFilter); 94 paint.setAlpha(fAlpha); 95 96 for (int i = 0; i < N; i++) { 97 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint); 98 } 99 } 100 101private: 102 typedef SkBenchmark INHERITED; 103}; 104 105DEF_BENCH(return new BitmapRectBench(p, 0xFF, false, false)) 106DEF_BENCH(return new BitmapRectBench(p, 0x80, false, false)) 107DEF_BENCH(return new BitmapRectBench(p, 0xFF, true, false)) 108DEF_BENCH(return new BitmapRectBench(p, 0x80, true, false)) 109 110DEF_BENCH(return new BitmapRectBench(p, 0xFF, false, true)) 111DEF_BENCH(return new BitmapRectBench(p, 0xFF, true, true)) 112