blob: a24334e60d3b459d361b1ef701cdb75375e3b2aa [file] [log] [blame]
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +00001/*
2 * Copyright 2014 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#ifndef image_expectations_DEFINED
9#define image_expectations_DEFINED
10
11#include "SkBitmap.h"
12#include "SkJSONCPP.h"
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +000013#include "SkOSFile.h"
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000014#include "SkRefCnt.h"
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000015
16namespace sk_tools {
17
18 /**
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000019 * The digest of an image (either an image we have generated locally, or an image expectation).
20 *
21 * Currently, this is always a uint64_t hash digest of an SkBitmap.
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000022 */
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000023 class ImageDigest : public SkRefCnt {
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000024 public:
25 /**
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000026 * Create an ImageDigest of a bitmap.
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000027 *
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000028 * Note that this is an expensive operation, because it has to examine all pixels in
29 * the bitmap. You may wish to consider using the BitmapAndDigest class, which will
30 * compute the ImageDigest lazily.
31 *
32 * @param bitmap image to get the digest of
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000033 */
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000034 explicit ImageDigest(const SkBitmap &bitmap);
35
36 /**
37 * Create an ImageDigest using a hashType/hashValue pair.
38 *
39 * @param hashType the algorithm used to generate the hash; for now, only
40 * kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5 is allowed.
41 * @param hashValue the value generated by the hash algorithm for a particular image.
42 */
43 explicit ImageDigest(const SkString &hashType, uint64_t hashValue);
44
45 /**
46 * Returns the hash digest type as an SkString.
47 *
48 * For now, this always returns kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5 .
49 */
50 SkString getHashType() const;
51
52 /**
53 * Returns the hash digest value as a uint64_t.
54 */
55 uint64_t getHashValue() const;
56
57 private:
58 uint64_t fHashValue;
59 };
60
61 /**
62 * Container that holds a reference to an SkBitmap and computes its ImageDigest lazily.
63 *
64 * Computing the ImageDigest can be expensive, so this can help you postpone (or maybe even
65 * avoid) that work.
66 */
67 class BitmapAndDigest {
68 public:
69 explicit BitmapAndDigest(const SkBitmap &bitmap);
70
71 const ImageDigest *getImageDigestPtr();
72 const SkBitmap *getBitmapPtr() const;
73 private:
74 const SkBitmap fBitmap;
75 SkAutoTUnref<ImageDigest> fImageDigestRef;
76 };
77
78 /**
79 * Collects ImageDigests of actually rendered images, perhaps comparing to expectations.
80 */
81 class ImageResultsAndExpectations {
82 public:
83 /**
84 * Adds expectations from a JSON file, returning true if successful.
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +000085 *
86 * If the file exists but is empty, it succeeds, and there will be no expectations.
87 * If the file does not exist, this will fail.
88 *
89 * Reasoning:
90 * Generating expectations the first time can be a tricky chicken-and-egg
91 * proposition. "I need actual results to turn into expectations... but the only
92 * way to get actual results is to run the tool, and the tool won't run without
93 * expectations!"
94 * We could make the tool run even if there is no expectations file at all, but it's
95 * better for the tool to fail if the expectations file is not found--that will tell us
96 * quickly if files are not being copied around as they should be.
97 * Creating an empty file is an easy way to break the chicken-and-egg cycle and generate
98 * the first real expectations.
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000099 */
100 bool readExpectationsFile(const char *jsonPath);
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000101
102 /**
103 * Adds this image to the summary of results.
104 *
105 * @param sourceName name of the source file that generated this result
106 * @param fileName relative path to the image output file on local disk
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000107 * @param digest description of the image's contents
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000108 * @param tileNumber if not NULL, pointer to tile number
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000109 */
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000110 void add(const char *sourceName, const char *fileName, const ImageDigest &digest,
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000111 const int *tileNumber=NULL);
112
113 /**
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000114 * Returns true if this test result matches its expectations.
115 * If there are no expectations for this test result, this will return false.
116 *
117 * @param sourceName name of the source file that generated this result
118 * @param digest description of the image's contents
119 * @param tileNumber if not NULL, pointer to tile number
120 */
121 bool matchesExpectation(const char *sourceName, const ImageDigest &digest,
122 const int *tileNumber=NULL);
123
124 /**
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000125 * Writes the summary (as constructed so far) to a file.
126 *
127 * @param filename path to write the summary to
128 */
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000129 void writeToFile(const char *filename) const;
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000130
131 private:
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000132
133 /**
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000134 * Read the file contents from filePtr and parse them into jsonRoot.
135 *
136 * It is up to the caller to close filePtr after this is done.
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000137 *
138 * Returns true if successful.
139 */
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000140 static bool Parse(SkFILE* filePtr, Json::Value *jsonRoot);
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000141
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000142 Json::Value fActualResults;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000143 Json::Value fExpectedJsonRoot;
144 Json::Value fExpectedResults;
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +0000145 };
146
147} // namespace sk_tools
148
149#endif // image_expectations_DEFINED