1 2 /* 3 * Copyright 2013 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 9 #include "SkBenchmark.h" 10 #include "SkCanvas.h" 11 #include "SkConfig8888.h" 12 #include "SkString.h" 13 14 class PremulAndUnpremulAlphaOpsBench : public SkBenchmark { 15 public: 16 PremulAndUnpremulAlphaOpsBench(void* param, SkCanvas::Config8888 config) 17 : INHERITED(param) { 18 fUnPremulConfig = config; 19 fName.printf("premul_and_unpremul_alpha_%s", 20 (config == SkCanvas::kRGBA_Unpremul_Config8888) ? 21 "RGBA8888" : "Native8888"); 22 } 23 24 protected: 25 virtual const char* onGetName() SK_OVERRIDE { 26 return fName.c_str(); 27 } 28 29 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 30 canvas->clear(SK_ColorBLACK); 31 SkISize size = canvas->getDeviceSize(); 32 33 SkBitmap bmp1; 34 bmp1.setConfig(SkBitmap::kARGB_8888_Config, size.width(), 35 size.height()); 36 bmp1.allocPixels(); 37 SkAutoLockPixels alp(bmp1); 38 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp1.getPixels()); 39 for (int h = 0; h < size.height(); ++h) { 40 for (int w = 0; w < size.width(); ++w) 41 pixels[h * size.width() + w] = SkPackConfig8888(fUnPremulConfig, 42 h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF); 43 } 44 45 SkBitmap bmp2; 46 bmp2.setConfig(SkBitmap::kARGB_8888_Config, size.width(), 47 size.height()); 48 49 static const int kLoopCount = SkBENCHLOOP(10); 50 for (int loop = 0; loop < kLoopCount; ++loop) { 51 // Unpremul -> Premul 52 canvas->writePixels(bmp1, 0, 0, fUnPremulConfig); 53 // Premul -> Unpremul 54 canvas->readPixels(&bmp2, 0, 0, fUnPremulConfig); 55 } 56 } 57 58 private: 59 SkCanvas::Config8888 fUnPremulConfig; 60 SkString fName; 61 typedef SkBenchmark INHERITED; 62 }; 63 64 static SkBenchmark* fact0(void* p) { 65 return new PremulAndUnpremulAlphaOpsBench(p, 66 SkCanvas::kRGBA_Unpremul_Config8888); 67 } 68 static SkBenchmark* fact1(void* p) { 69 return new PremulAndUnpremulAlphaOpsBench(p, 70 SkCanvas::kNative_Unpremul_Config8888); 71 } 72 73 static BenchRegistry gReg0(fact0); 74 static BenchRegistry gReg1(fact1); 75