• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bench/Benchmark.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColorSpace.h"
12 
13 // Time variants of read-pixels
14 //  [ colortype ][ alphatype ][ colorspace ]
15 //  Different combinations can trigger fast or slow paths in the impls
16 //
17 class ReadPixBench : public Benchmark {
18 public:
ReadPixBench(SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)19     ReadPixBench(SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
20         : fCT(ct), fAT(at), fCS(cs)
21     {
22         fName.printf("readpix_%s_%s_%s",
23                      at == kPremul_SkAlphaType ? "pm" : "um",
24                      ct == kRGBA_8888_SkColorType ? "rgba" : "bgra",
25                      cs ? "srgb" : "null");
26     }
27 
28 protected:
onGetName()29     const char* onGetName() override {
30         return fName.c_str();
31     }
32 
onDraw(int loops,SkCanvas * canvas)33     void onDraw(int loops, SkCanvas* canvas) override {
34         canvas->clear(0x80000000);
35 
36         SkISize size = canvas->getBaseLayerSize();
37 
38         auto info = SkImageInfo::Make(size, fCT, fAT, fCS);
39         SkBitmap bitmap;
40         bitmap.allocPixels(info);
41 
42         for (int i = 0; i < loops; i++) {
43             canvas->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
44         }
45     }
46 
47 private:
48     SkColorType fCT;
49     SkAlphaType fAT;
50     sk_sp<SkColorSpace> fCS;
51     SkString fName;
52     using INHERITED = Benchmark;
53 };
54 DEF_BENCH( return new ReadPixBench(kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); )
55 DEF_BENCH( return new ReadPixBench(kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr); )
56 DEF_BENCH( return new ReadPixBench(kRGBA_8888_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); )
57 DEF_BENCH( return new ReadPixBench(kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB()); )
58 
59 DEF_BENCH( return new ReadPixBench(kBGRA_8888_SkColorType, kPremul_SkAlphaType, nullptr); )
60 DEF_BENCH( return new ReadPixBench(kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr); )
61 DEF_BENCH( return new ReadPixBench(kBGRA_8888_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); )
62 DEF_BENCH( return new ReadPixBench(kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB()); )
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 #include "include/core/SkBitmap.h"
66 #include "src/core/SkPixmapPriv.h"
67 
68 class PixmapOrientBench : public Benchmark {
69 public:
PixmapOrientBench()70     PixmapOrientBench() {}
71 
72 protected:
onDelayedSetup()73     void onDelayedSetup() override {
74         const SkImageInfo info = SkImageInfo::MakeN32Premul(2048, 1024);
75         fSrc.allocPixels(info);
76         fSrc.eraseColor(SK_ColorBLACK);
77         fDst.allocPixels(info.makeDimensions(info.dimensions()));
78     }
79 
onGetName()80     const char* onGetName() override {
81         return "orient_pixmap";
82     }
83 
isSuitableFor(Backend backend)84     bool isSuitableFor(Backend backend) override {
85         return backend == kNonRendering_Backend;
86     }
87 
onDraw(int loops,SkCanvas *)88     void onDraw(int loops, SkCanvas*) override {
89         SkPixmap src, dst;
90         fSrc.peekPixels(&src);
91         fDst.peekPixels(&dst);
92         for (int i = 0; i < loops; ++i) {
93             SkPixmapPriv::Orient(dst, src, kTopRight_SkEncodedOrigin);
94         }
95     }
96 
97 private:
98     SkBitmap fSrc, fDst;
99 
100     using INHERITED = Benchmark;
101 };
102 DEF_BENCH( return new PixmapOrientBench(); )
103 
104 
105 class GetAlphafBench : public Benchmark {
106     SkString fName;
107     SkColorType fCT;
108 public:
GetAlphafBench(SkColorType ct,const char label[])109     GetAlphafBench(SkColorType ct, const char label[]) : fCT(ct) {
110         fName.printf("getalphaf_%s", label);
111     }
112 
113 protected:
onDelayedSetup()114     void onDelayedSetup() override {
115         fBM.allocPixels(SkImageInfo::Make(1024, 1024, fCT, kPremul_SkAlphaType));
116         fBM.eraseColor(0x88112233);
117     }
118 
onGetName()119     const char* onGetName() override {
120         return fName.c_str();
121     }
122 
isSuitableFor(Backend backend)123     bool isSuitableFor(Backend backend) override {
124         return backend == kNonRendering_Backend;
125     }
126 
onDraw(int loops,SkCanvas *)127     void onDraw(int loops, SkCanvas*) override {
128         for (int i = 0; i < loops; ++i) {
129             for (int y = 0; y < fBM.height(); ++y) {
130                 for (int x = 0; x < fBM.width(); ++x) {
131                     fBM.getAlphaf(x, y);
132                 }
133             }
134         }
135     }
136 
137 private:
138     SkBitmap fBM;
139 
140     using INHERITED = Benchmark;
141 };
142 DEF_BENCH( return new GetAlphafBench(kN32_SkColorType, "rgba"); )
143 DEF_BENCH( return new GetAlphafBench(kRGB_888x_SkColorType, "rgbx"); )
144 DEF_BENCH( return new GetAlphafBench(kRGBA_F16_SkColorType, "f16"); )
145 DEF_BENCH( return new GetAlphafBench(kRGBA_F32_SkColorType, "f32"); )
146 
147