blob: 3ce2fb0afedf1b833a545ef7d1b4348e338117ec [file] [log] [blame]
scroggo@google.com2bbc2c92013-06-14 15:33:20 +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"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +00009#include "SkBitmap.h"
10#include "SkData.h"
11#include "SkForceLinking.h"
12#include "SkImageDecoder.h"
13#include "SkOSFile.h"
14#include "SkStream.h"
15#include "SkString.h"
16
17__SK_FORCE_IMAGE_DECODER_LINKING;
18
19class SkCanvas;
20
tfarinaf168b862014-06-19 12:32:29 -070021class ImageDecodeBench : public Benchmark {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000022public:
23 ImageDecodeBench(void* p, const char* filename)
mtklein@google.com410e6e82013-09-13 19:52:27 +000024 : fName("image_decode_")
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000025 , fFilename(filename)
26 , fStream()
27 , fValid(false) {
tfarinaa8e2e152014-07-28 19:26:58 -070028 fName.append(SkOSPath::Basename(filename));
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000029 }
30
31 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
32 return backend == kNonRendering_Backend;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000033 }
34
35protected:
36 virtual const char* onGetName() SK_OVERRIDE {
37 return fName.c_str();
38 }
39
40 virtual void onPreDraw() SK_OVERRIDE {
41 SkFILEStream fileStream(fFilename.c_str());
42 fValid = fileStream.isValid() && fileStream.getLength() > 0;
43 if (fValid) {
44 const size_t size = fileStream.getLength();
45 void* data = sk_malloc_throw(size);
46 if (fileStream.read(data, size) < size) {
47 fValid = false;
48 } else {
49 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
50 fStream.setData(skdata.get());
51 }
52 }
53 }
54
commit-bot@chromium.org33614712013-12-03 18:17:16 +000055 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000056#ifdef SK_DEBUG
57 if (!fValid) {
58 SkDebugf("stream was invalid: %s\n", fName.c_str());
59 return;
60 }
61#endif
62 // Decode a bunch of times
63 SkBitmap bm;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000064 for (int i = 0; i < loops; ++i) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000065 SkDEBUGCODE(bool success =) SkImageDecoder::DecodeStream(&fStream, &bm);
66#ifdef SK_DEBUG
67 if (!success) {
68 SkDebugf("failed to decode %s\n", fName.c_str());
69 return;
70 }
71#endif
72 SkDEBUGCODE(success =) fStream.rewind();
73#ifdef SK_DEBUG
74 if (!success) {
75 SkDebugf("failed to rewind %s\n", fName.c_str());
76 return;
77 }
78#endif
79 }
80 }
81
82private:
83 SkString fName;
84 const SkString fFilename;
85 SkMemoryStream fStream;
86 bool fValid;
87
tfarinaf168b862014-06-19 12:32:29 -070088 typedef Benchmark INHERITED;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000089};
90
91// These are files which call decodePalette
mtklein@google.com410e6e82013-09-13 19:52:27 +000092//DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, ("/usr/local/google/home/scroggo/Downloads/images/hal_163x90.png")); )
93//DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, ("/usr/local/google/home/scroggo/Downloads/images/box_19_top-left.png")); )