blob: 3a61163862082830bfcbcc7b3d5f8d421ff9b335 [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
8#include "SkBenchmark.h"
9#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
21class ImageDecodeBench : public SkBenchmark {
22public:
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) {
28 fName.append(SkOSPath::SkBasename(filename));
29 fIsRendering = false;
30 }
31
32protected:
33 virtual const char* onGetName() SK_OVERRIDE {
34 return fName.c_str();
35 }
36
37 virtual void onPreDraw() SK_OVERRIDE {
38 SkFILEStream fileStream(fFilename.c_str());
39 fValid = fileStream.isValid() && fileStream.getLength() > 0;
40 if (fValid) {
41 const size_t size = fileStream.getLength();
42 void* data = sk_malloc_throw(size);
43 if (fileStream.read(data, size) < size) {
44 fValid = false;
45 } else {
46 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
47 fStream.setData(skdata.get());
48 }
49 }
50 }
51
52 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
53#ifdef SK_DEBUG
54 if (!fValid) {
55 SkDebugf("stream was invalid: %s\n", fName.c_str());
56 return;
57 }
58#endif
59 // Decode a bunch of times
60 SkBitmap bm;
mtklein@google.comc2897432013-09-10 19:23:38 +000061 for (int i = 0; i < this->getLoops(); ++i) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000062 SkDEBUGCODE(bool success =) SkImageDecoder::DecodeStream(&fStream, &bm);
63#ifdef SK_DEBUG
64 if (!success) {
65 SkDebugf("failed to decode %s\n", fName.c_str());
66 return;
67 }
68#endif
69 SkDEBUGCODE(success =) fStream.rewind();
70#ifdef SK_DEBUG
71 if (!success) {
72 SkDebugf("failed to rewind %s\n", fName.c_str());
73 return;
74 }
75#endif
76 }
77 }
78
79private:
80 SkString fName;
81 const SkString fFilename;
82 SkMemoryStream fStream;
83 bool fValid;
84
85 typedef SkBenchmark INHERITED;
86};
87
88// These are files which call decodePalette
mtklein@google.com410e6e82013-09-13 19:52:27 +000089//DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, ("/usr/local/google/home/scroggo/Downloads/images/hal_163x90.png")); )
90//DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, ("/usr/local/google/home/scroggo/Downloads/images/box_19_top-left.png")); )