blob: 4e2dbeba33c85ca32d64cc8291f684ed0abdcfc8 [file] [log] [blame]
commit-bot@chromium.org8f426972013-08-16 00:40:34 +00001/*
2 * Copyright 2013 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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
Florin Malitaab244f02017-05-03 19:16:58 +00009#include "SkBitmap.h"
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000010#include "SkCanvas.h"
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000011#include "SkString.h"
12
tfarinaf168b862014-06-19 12:32:29 -070013class WritePixelsBench : public Benchmark {
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000014public:
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000015 WritePixelsBench(SkColorType ct, SkAlphaType at)
16 : fColorType(ct)
17 , fAlphaType(at)
18 , fName("writepix")
19 {
20 switch (ct) {
21 case kRGBA_8888_SkColorType:
22 fName.append("_RGBA");
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000023 break;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000024 case kBGRA_8888_SkColorType:
25 fName.append("_BGRA");
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000026 break;
skia.committer@gmail.comc9917c02013-08-16 07:01:49 +000027 default:
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000028 SkASSERT(0);
29 break;
30 }
31 switch (at) {
32 case kPremul_SkAlphaType:
33 fName.append("_PM");
34 break;
35 case kUnpremul_SkAlphaType:
36 fName.append("_UPM");
37 break;
38 default:
39 SkASSERT(0);
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000040 break;
41 }
42 }
43
44protected:
mtklein36352bf2015-03-25 18:17:31 -070045 const char* onGetName() override {
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000046 return fName.c_str();
47 }
48
mtkleina1ebeb22015-10-01 09:43:39 -070049 void onDraw(int loops, SkCanvas* canvas) override {
Mike Reed3661bc92017-02-22 13:21:42 -050050 SkISize size = canvas->getBaseLayerSize();
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000051
52 canvas->clear(0xFFFF0000);
53
54 SkBitmap bmp;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000055 bmp.allocN32Pixels(size.width(), size.height());
Mike Reed12e946b2017-04-17 10:53:29 -040056 canvas->readPixels(bmp, 0, 0);
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000057
reede5ea5002014-09-03 11:54:58 -070058 SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(), fColorType, fAlphaType);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +000059
commit-bot@chromium.org33614712013-12-03 18:17:16 +000060 for (int loop = 0; loop < loops; ++loop) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000061 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0);
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000062 }
63 }
64
65private:
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000066 SkColorType fColorType;
67 SkAlphaType fAlphaType;
68 SkString fName;
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000069
tfarinaf168b862014-06-19 12:32:29 -070070 typedef Benchmark INHERITED;
commit-bot@chromium.org8f426972013-08-16 00:40:34 +000071};
72
73//////////////////////////////////////////////////////////////////////////////
74
halcanary385fe4d2015-08-26 13:07:48 -070075DEF_BENCH(return new WritePixelsBench(kRGBA_8888_SkColorType, kPremul_SkAlphaType);)
76DEF_BENCH(return new WritePixelsBench(kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);)