1 /* 2 * Copyright 2012 The Android Open Source Project 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 "Benchmark.h" 9 #include "SkBitmap.h" 10 #include "SkCanvas.h" 11 12 13 /** 14 * This bench mark tests the use case where the user writes the a canvas 15 * and then reads small chunks from it repeatedly. This can cause trouble 16 * for the GPU as readbacks are very expensive. 17 */ 18 class ReadPixBench : public Benchmark { 19 public: ReadPixBench()20 ReadPixBench() {} 21 22 protected: onGetName()23 const char* onGetName() override { 24 return "readpix"; 25 } 26 onDraw(int loops,SkCanvas * canvas)27 void onDraw(int loops, SkCanvas* canvas) override { 28 canvas->clear(SK_ColorBLACK); 29 30 SkISize size = canvas->getBaseLayerSize(); 31 32 int offX = (size.width() - kWindowSize) / kNumStepsX; 33 int offY = (size.height() - kWindowSize) / kNumStepsY; 34 35 SkPaint paint; 36 37 paint.setColor(SK_ColorBLUE); 38 39 canvas->drawCircle(SkIntToScalar(size.width()/2), 40 SkIntToScalar(size.height()/2), 41 SkIntToScalar(size.width()/2), 42 paint); 43 44 SkBitmap bitmap; 45 46 bitmap.allocPixels(SkImageInfo::MakeN32Premul(kWindowSize, kWindowSize)); 47 48 for (int i = 0; i < loops; i++) { 49 for (int x = 0; x < kNumStepsX; ++x) { 50 for (int y = 0; y < kNumStepsY; ++y) { 51 canvas->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 52 x * offX, y * offY); 53 } 54 } 55 } 56 } 57 58 private: 59 static const int kNumStepsX = 30; 60 static const int kNumStepsY = 30; 61 static const int kWindowSize = 5; 62 63 typedef Benchmark INHERITED; 64 }; 65 DEF_BENCH( return new ReadPixBench(); ) 66 67 //////////////////////////////////////////////////////////////////////////////// 68 #include "SkBitmap.h" 69 #include "SkPixmapPriv.h" 70 71 class PixmapOrientBench : public Benchmark { 72 public: PixmapOrientBench()73 PixmapOrientBench() {} 74 75 protected: onDelayedSetup()76 void onDelayedSetup() override { 77 const SkImageInfo info = SkImageInfo::MakeN32Premul(2048, 1024); 78 fSrc.allocPixels(info); 79 fSrc.eraseColor(SK_ColorBLACK); 80 fDst.allocPixels(info.makeWH(info.height(), info.width())); 81 } 82 onGetName()83 const char* onGetName() override { 84 return "orient_pixmap"; 85 } 86 isSuitableFor(Backend backend)87 bool isSuitableFor(Backend backend) override { 88 return backend == kNonRendering_Backend; 89 } 90 onDraw(int loops,SkCanvas *)91 void onDraw(int loops, SkCanvas*) override { 92 SkPixmap src, dst; 93 fSrc.peekPixels(&src); 94 fDst.peekPixels(&dst); 95 for (int i = 0; i < loops; ++i) { 96 SkPixmapPriv::Orient(dst, src, kTopRight_SkEncodedOrigin); 97 } 98 } 99 100 private: 101 SkBitmap fSrc, fDst; 102 103 typedef Benchmark INHERITED; 104 }; 105 DEF_BENCH( return new PixmapOrientBench(); ) 106 107 108 class GetAlphafBench : public Benchmark { 109 SkString fName; 110 SkColorType fCT; 111 public: GetAlphafBench(SkColorType ct,const char label[])112 GetAlphafBench(SkColorType ct, const char label[]) : fCT(ct) { 113 fName.printf("getalphaf_%s", label); 114 } 115 116 protected: onDelayedSetup()117 void onDelayedSetup() override { 118 fBM.allocPixels(SkImageInfo::Make(1024, 1024, fCT, kPremul_SkAlphaType)); 119 fBM.eraseColor(0x88112233); 120 } 121 onGetName()122 const char* onGetName() override { 123 return fName.c_str(); 124 } 125 isSuitableFor(Backend backend)126 bool isSuitableFor(Backend backend) override { 127 return backend == kNonRendering_Backend; 128 } 129 onDraw(int loops,SkCanvas *)130 void onDraw(int loops, SkCanvas*) override { 131 for (int i = 0; i < loops; ++i) { 132 for (int y = 0; y < fBM.height(); ++y) { 133 for (int x = 0; x < fBM.width(); ++x) { 134 fBM.getAlphaf(x, y); 135 } 136 } 137 } 138 } 139 140 private: 141 SkBitmap fBM; 142 143 typedef Benchmark INHERITED; 144 }; 145 DEF_BENCH( return new GetAlphafBench(kN32_SkColorType, "rgba"); ) 146 DEF_BENCH( return new GetAlphafBench(kRGB_888x_SkColorType, "rgbx"); ) 147 DEF_BENCH( return new GetAlphafBench(kRGBA_F16_SkColorType, "f16"); ) 148 DEF_BENCH( return new GetAlphafBench(kRGBA_F32_SkColorType, "f32"); ) 149 150