aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-05-08 14:48:44 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-05-08 14:48:44 +0000
commitee18f2a3c92f3df3998cc93ffc438a9453281c6b (patch)
treebaa9f94223b1789bafe1a99f7ddcbd66035060db
parent8fec4099d897e87df8152c2a090c22dc6c45cd6c (diff)
extract some common code from PictureRenderer
R=borenet@google.com Author: epoger@google.com Review URL: https://codereview.chromium.org/273703006 git-svn-id: http://skia.googlecode.com/svn/trunk@14638 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--gm/gm_expectations.cpp24
-rw-r--r--gm/gm_expectations.h43
-rw-r--r--gyp/gm.gyp1
-rw-r--r--gyp/tools.gyp5
-rw-r--r--gyp/utils.gyp2
-rw-r--r--src/utils/SkDataUtils.cpp28
-rw-r--r--src/utils/SkDataUtils.h63
-rw-r--r--tools/PictureRenderer.cpp67
-rw-r--r--tools/PictureRenderer.h41
-rw-r--r--tools/image_expectations.cpp82
-rw-r--r--tools/image_expectations.h56
-rw-r--r--tools/render_pictures_main.cpp2
12 files changed, 251 insertions, 163 deletions
diff --git a/gm/gm_expectations.cpp b/gm/gm_expectations.cpp
index f46a572458..40bb2b4f90 100644
--- a/gm/gm_expectations.cpp
+++ b/gm/gm_expectations.cpp
@@ -3,10 +3,14 @@
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
+ *
+ * TODO(epoger): Combine this with tools/image_expectations.cpp, or eliminate one of the two.
*/
#include "gm_expectations.h"
#include "SkBitmapHasher.h"
+#include "SkData.h"
+#include "SkDataUtils.h"
#include "SkImageDecoder.h"
#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
@@ -219,24 +223,6 @@ namespace skiagm {
return Expectations(fJsonExpectedResults[testName]);
}
- /*static*/ SkData* JsonExpectationsSource::ReadIntoSkData(SkStream &stream, size_t maxBytes) {
- if (0 == maxBytes) {
- return SkData::NewEmpty();
- }
- char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes));
- char* bufPtr = bufStart;
- size_t bytesRemaining = maxBytes;
- while (bytesRemaining > 0) {
- size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining);
- if (0 == bytesReadThisTime) {
- break;
- }
- bytesRemaining -= bytesReadThisTime;
- bufPtr += bytesReadThisTime;
- }
- return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining);
- }
-
/*static*/ bool JsonExpectationsSource::Parse(const char *jsonPath, Json::Value *jsonRoot) {
SkFILEStream inFile(jsonPath);
if (!inFile.isValid()) {
@@ -245,7 +231,7 @@ namespace skiagm {
return false;
}
- SkAutoDataUnref dataRef(ReadFileIntoSkData(inFile));
+ SkAutoDataUnref dataRef(SkDataUtils::ReadFileIntoSkData(inFile));
if (NULL == dataRef.get()) {
SkDebugf("error reading JSON file %s\n", jsonPath);
DEBUGFAIL_SEE_STDERR;
diff --git a/gm/gm_expectations.h b/gm/gm_expectations.h
index d454732dad..7092127338 100644
--- a/gm/gm_expectations.h
+++ b/gm/gm_expectations.h
@@ -3,6 +3,8 @@
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
+ *
+ * TODO(epoger): Combine this with tools/image_expectations.h, or eliminate one of the two.
*/
#ifndef gm_expectations_DEFINED
#define gm_expectations_DEFINED
@@ -216,47 +218,6 @@ namespace skiagm {
private:
/**
- * Read as many bytes as possible (up to maxBytes) from the stream into
- * an SkData object.
- *
- * If the returned SkData contains fewer than maxBytes, then EOF has been
- * reached and no more data would be available from subsequent calls.
- * (If EOF has already been reached, then this call will return an empty
- * SkData object immediately.)
- *
- * If there are fewer than maxBytes bytes available to read from the
- * stream, but the stream has not been closed yet, this call will block
- * until there are enough bytes to read or the stream has been closed.
- *
- * It is up to the caller to call unref() on the returned SkData object
- * once the data is no longer needed, so that the underlying buffer will
- * be freed. For example:
- *
- * {
- * size_t maxBytes = 256;
- * SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes));
- * if (NULL != dataRef.get()) {
- * size_t bytesActuallyRead = dataRef.get()->size();
- * // use the data...
- * }
- * }
- * // underlying buffer has been freed, thanks to auto unref
- *
- */
- // TODO(epoger): Move this, into SkStream.[cpp|h] as attempted in
- // https://codereview.appspot.com/7300071 ?
- // And maybe ReadFileIntoSkData() also?
- static SkData* ReadIntoSkData(SkStream &stream, size_t maxBytes);
-
- /**
- * Wrapper around ReadIntoSkData for files: reads the entire file into
- * an SkData object.
- */
- static SkData* ReadFileIntoSkData(SkFILEStream &stream) {
- return ReadIntoSkData(stream, stream.getLength());
- }
-
- /**
* Read the file contents from jsonPath and parse them into jsonRoot.
*
* Returns true if successful.
diff --git a/gyp/gm.gyp b/gyp/gm.gyp
index 3533aa5ce9..546052569c 100644
--- a/gyp/gm.gyp
+++ b/gyp/gm.gyp
@@ -17,6 +17,7 @@
'dependencies': [
'skia_lib.gyp:skia_lib',
'jsoncpp.gyp:jsoncpp',
+ 'utils.gyp:utils',
],
'direct_dependent_settings': {
'include_dirs': [
diff --git a/gyp/tools.gyp b/gyp/tools.gyp
index 769cf6cd2f..e3ed7d5d41 100644
--- a/gyp/tools.gyp
+++ b/gyp/tools.gyp
@@ -369,6 +369,8 @@
'target_name': 'picture_renderer',
'type': 'static_library',
'sources': [
+ '../tools/image_expectations.h',
+ '../tools/image_expectations.cpp',
'../tools/LazyDecodeBitmap.cpp',
'../tools/PictureRenderer.h',
'../tools/PictureRenderer.cpp',
@@ -388,7 +390,7 @@
],
'direct_dependent_settings': {
'include_dirs': [
- # needed for JSON headers used within PictureRenderer.h
+ # needed for JSON headers used within image_expectations.h
'../third_party/externals/jsoncpp-chromium/overrides/include/',
'../third_party/externals/jsoncpp/include/',
],
@@ -398,6 +400,7 @@
'jsoncpp.gyp:jsoncpp',
'skia_lib.gyp:skia_lib',
'tools.gyp:picture_utils',
+ 'utils.gyp:utils',
],
'conditions': [
['skia_gpu == 1',
diff --git a/gyp/utils.gyp b/gyp/utils.gyp
index 5b3d9ea98c..b61a1069f7 100644
--- a/gyp/utils.gyp
+++ b/gyp/utils.gyp
@@ -73,6 +73,8 @@
'../src/utils/SkCanvasStateUtils.cpp',
'../src/utils/SkCubicInterval.cpp',
'../src/utils/SkCullPoints.cpp',
+ '../src/utils/SkDataUtils.cpp',
+ '../src/utils/SkDataUtils.h',
'../src/utils/SkDeferredCanvas.cpp',
'../src/utils/SkDumpCanvas.cpp',
'../src/utils/SkEventTracer.cpp',
diff --git a/src/utils/SkDataUtils.cpp b/src/utils/SkDataUtils.cpp
new file mode 100644
index 0000000000..c5544f71ba
--- /dev/null
+++ b/src/utils/SkDataUtils.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkData.h"
+#include "SkDataUtils.h"
+#include "SkStream.h"
+
+/*static*/ SkData* SkDataUtils::ReadIntoSkData(SkStream &stream, size_t maxBytes) {
+ if (0 == maxBytes) {
+ return SkData::NewEmpty();
+ }
+ char* bufStart = reinterpret_cast<char *>(sk_malloc_throw(maxBytes));
+ char* bufPtr = bufStart;
+ size_t bytesRemaining = maxBytes;
+ while (bytesRemaining > 0) {
+ size_t bytesReadThisTime = stream.read(bufPtr, bytesRemaining);
+ if (0 == bytesReadThisTime) {
+ break;
+ }
+ bytesRemaining -= bytesReadThisTime;
+ bufPtr += bytesReadThisTime;
+ }
+ return SkData::NewFromMalloc(bufStart, maxBytes - bytesRemaining);
+}
diff --git a/src/utils/SkDataUtils.h b/src/utils/SkDataUtils.h
new file mode 100644
index 0000000000..fc237f7b1a
--- /dev/null
+++ b/src/utils/SkDataUtils.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkDataUtils_DEFINED
+#define SkDataUtils_DEFINED
+
+#include "SkData.h"
+#include "SkStream.h"
+
+/**
+ * Static class that performs various operations on SkData objects.
+ *
+ * EPOGER: Before committing, add unittests for these methods.
+ *
+ * TODO(epoger): Move these methods into SkStream.[cpp|h], as attempted in
+ * https://codereview.appspot.com/7300071 ?
+ */
+class SkDataUtils {
+public:
+ /**
+ * Read as many bytes as possible (up to maxBytes) from the stream into
+ * an SkData object.
+ *
+ * If the returned SkData contains fewer than maxBytes, then EOF has been
+ * reached and no more data would be available from subsequent calls.
+ * (If EOF has already been reached, then this call will return an empty
+ * SkData object immediately.)
+ *
+ * If there are fewer than maxBytes bytes available to read from the
+ * stream, but the stream has not been closed yet, this call will block
+ * until there are enough bytes to read or the stream has been closed.
+ *
+ * It is up to the caller to call unref() on the returned SkData object
+ * once the data is no longer needed, so that the underlying buffer will
+ * be freed. For example:
+ *
+ * {
+ * size_t maxBytes = 256;
+ * SkAutoDataUnref dataRef(readIntoSkData(stream, maxBytes));
+ * if (NULL != dataRef.get()) {
+ * size_t bytesActuallyRead = dataRef.get()->size();
+ * // use the data...
+ * }
+ * }
+ * // underlying buffer has been freed, thanks to auto unref
+ */
+ static SkData* ReadIntoSkData(SkStream &stream, size_t maxBytes);
+
+ /**
+ * Wrapper around ReadIntoSkData for files: reads the entire file into
+ * an SkData object.
+ */
+ static SkData* ReadFileIntoSkData(SkFILEStream &stream) {
+ return ReadIntoSkData(stream, stream.getLength());
+ }
+
+};
+
+#endif
diff --git a/tools/PictureRenderer.cpp b/tools/PictureRenderer.cpp
index c712ae672f..b5dc0fc93d 100644
--- a/tools/PictureRenderer.cpp
+++ b/tools/PictureRenderer.cpp
@@ -48,67 +48,6 @@ enum {
kDefaultTileHeight = 256
};
-/*
- * TODO(epoger): Make constant strings consistent instead of mixing hypenated and camel-caps.
- *
- * TODO(epoger): Similar constants are already maintained in 2 other places:
- * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place.
- * Figure out a way to share the definitions instead.
- *
- * Note that, as of https://codereview.chromium.org/226293002 , the JSON
- * schema used here has started to differ from the one in gm_expectations.cpp .
- * TODO(epoger): Consider getting GM and render_pictures to use the same JSON
- * output module.
- */
-const static char kJsonKey_ActualResults[] = "actual-results";
-const static char kJsonKey_Header[] = "header";
-const static char kJsonKey_Header_Type[] = "type";
-const static char kJsonKey_Header_Revision[] = "revision"; // unique within Type
-const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
-const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
-const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
-const static char kJsonKey_Image_Filepath[] = "filepath";
-const static char kJsonKey_Source_TiledImages[] = "tiled-images";
-const static char kJsonKey_Source_WholeImage[] = "whole-image";
-// Values (not keys) that are written out by this JSON generator
-const static char kJsonValue_Header_Type[] = "ChecksummedImages";
-const static int kJsonValue_Header_Revision = 1;
-const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
-const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
-
-void ImageResultsSummary::add(const char *sourceName, const char *fileName, uint64_t hash,
- const int *tileNumber) {
- Json::Value image;
- image[kJsonKey_Image_ChecksumAlgorithm] = kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5;
- image[kJsonKey_Image_ChecksumValue] = Json::UInt64(hash);
- image[kJsonKey_Image_ComparisonResult] = kJsonValue_Image_ComparisonResult_NoComparison;
- image[kJsonKey_Image_Filepath] = fileName;
- if (NULL == tileNumber) {
- fActualResults[sourceName][kJsonKey_Source_WholeImage] = image;
- } else {
- fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = image;
- }
-}
-
-void ImageResultsSummary::add(const char *sourceName, const char *fileName, const SkBitmap& bitmap,
- const int *tileNumber) {
- uint64_t hash;
- SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash));
- this->add(sourceName, fileName, hash, tileNumber);
-}
-
-void ImageResultsSummary::writeToFile(const char *filename) {
- Json::Value header;
- header[kJsonKey_Header_Type] = kJsonValue_Header_Type;
- header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision;
- Json::Value root;
- root[kJsonKey_Header] = header;
- root[kJsonKey_ActualResults] = fActualResults;
- std::string jsonStdString = root.toStyledString();
- SkFILEWStream stream(filename);
- stream.write(jsonStdString.c_str(), jsonStdString.length());
-}
-
void PictureRenderer::init(SkPicture* pict, const SkString* outputDir,
const SkString* inputFilename, bool useChecksumBasedFilenames) {
this->CopyString(&fOutputDir, outputDir);
@@ -367,8 +306,10 @@ static bool write(SkCanvas* canvas, const SkString& outputDir, const SkString& i
generatedHash = true;
outputSubdirPtr = escapedInputFilename.c_str();
- outputFilename.set(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5);
- outputFilename.append("_");
+ // TODO(epoger): The string constant below will be removed when I land
+ // the second part of https://codereview.chromium.org/261313004/
+ // ('add --readJsonSummaryPath to render_pictures')
+ outputFilename.set("bitmap-64bitMD5_");
outputFilename.appendU64(hash);
} else {
outputFilename.set(escapedInputFilename);
diff --git a/tools/PictureRenderer.h b/tools/PictureRenderer.h
index 796162846b..445737e755 100644
--- a/tools/PictureRenderer.h
+++ b/tools/PictureRenderer.h
@@ -11,7 +11,6 @@
#include "SkCanvas.h"
#include "SkCountdown.h"
#include "SkDrawFilter.h"
-#include "SkJSONCPP.h"
#include "SkMath.h"
#include "SkPaint.h"
#include "SkPicture.h"
@@ -29,6 +28,8 @@
#include "GrContext.h"
#endif
+#include "image_expectations.h"
+
class SkBitmap;
class SkCanvas;
class SkGLContextHelper;
@@ -38,44 +39,6 @@ namespace sk_tools {
class TiledPictureRenderer;
-/**
- * Class for collecting image results (checksums) as we go.
- */
-class ImageResultsSummary {
-public:
- /**
- * Adds this image to the summary of results.
- *
- * @param sourceName name of the source file that generated this result
- * @param fileName relative path to the image output file on local disk
- * @param hash hash to store
- * @param tileNumber if not NULL, ptr to tile number
- */
- void add(const char *sourceName, const char *fileName, uint64_t hash,
- const int *tileNumber=NULL);
-
- /**
- * Adds this image to the summary of results.
- *
- * @param sourceName name of the source file that generated this result
- * @param fileName relative path to the image output file on local disk
- * @param bitmap bitmap to store the hash of
- * @param tileNumber if not NULL, ptr to tile number
- */
- void add(const char *sourceName, const char *fileName, const SkBitmap& bitmap,
- const int *tileNumber=NULL);
-
- /**
- * Writes the summary (as constructed so far) to a file.
- *
- * @param filename path to write the summary to
- */
- void writeToFile(const char *filename);
-
-private:
- Json::Value fActualResults;
-};
-
class PictureRenderer : public SkRefCnt {
public:
diff --git a/tools/image_expectations.cpp b/tools/image_expectations.cpp
new file mode 100644
index 0000000000..a1fa589a2f
--- /dev/null
+++ b/tools/image_expectations.cpp
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmap.h"
+#include "SkBitmapHasher.h"
+#include "SkData.h"
+#include "SkDataUtils.h"
+#include "SkJSONCPP.h"
+#include "SkOSFile.h"
+#include "SkStream.h"
+#include "SkTypes.h"
+
+#include "image_expectations.h"
+
+/*
+ * TODO(epoger): Make constant strings consistent instead of mixing hypenated and camel-caps.
+ *
+ * TODO(epoger): Similar constants are already maintained in 2 other places:
+ * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place.
+ * Figure out a way to share the definitions instead.
+ *
+ * Note that, as of https://codereview.chromium.org/226293002 , the JSON
+ * schema used here has started to differ from the one in gm_expectations.cpp .
+ * TODO(epoger): Consider getting GM and render_pictures to use the same JSON
+ * output module.
+ */
+const static char kJsonKey_ActualResults[] = "actual-results";
+const static char kJsonKey_Header[] = "header";
+const static char kJsonKey_Header_Type[] = "type";
+const static char kJsonKey_Header_Revision[] = "revision"; // unique within Type
+const static char kJsonKey_Image_ChecksumAlgorithm[] = "checksumAlgorithm";
+const static char kJsonKey_Image_ChecksumValue[] = "checksumValue";
+const static char kJsonKey_Image_ComparisonResult[] = "comparisonResult";
+const static char kJsonKey_Image_Filepath[] = "filepath";
+const static char kJsonKey_Source_TiledImages[] = "tiled-images";
+const static char kJsonKey_Source_WholeImage[] = "whole-image";
+// Values (not keys) that are written out by this JSON generator
+const static char kJsonValue_Header_Type[] = "ChecksummedImages";
+const static int kJsonValue_Header_Revision = 1;
+const static char kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5[] = "bitmap-64bitMD5";
+const static char kJsonValue_Image_ComparisonResult_NoComparison[] = "no-comparison";
+
+namespace sk_tools {
+
+ void ImageResultsSummary::add(const char *sourceName, const char *fileName, uint64_t hash,
+ const int *tileNumber) {
+ Json::Value image;
+ image[kJsonKey_Image_ChecksumAlgorithm] = kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5;
+ image[kJsonKey_Image_ChecksumValue] = Json::UInt64(hash);
+ image[kJsonKey_Image_ComparisonResult] = kJsonValue_Image_ComparisonResult_NoComparison;
+ image[kJsonKey_Image_Filepath] = fileName;
+ if (NULL == tileNumber) {
+ fActualResults[sourceName][kJsonKey_Source_WholeImage] = image;
+ } else {
+ fActualResults[sourceName][kJsonKey_Source_TiledImages][*tileNumber] = image;
+ }
+ }
+
+ void ImageResultsSummary::add(const char *sourceName, const char *fileName, const SkBitmap& bitmap,
+ const int *tileNumber) {
+ uint64_t hash;
+ SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash));
+ this->add(sourceName, fileName, hash, tileNumber);
+ }
+
+ void ImageResultsSummary::writeToFile(const char *filename) {
+ Json::Value header;
+ header[kJsonKey_Header_Type] = kJsonValue_Header_Type;
+ header[kJsonKey_Header_Revision] = kJsonValue_Header_Revision;
+ Json::Value root;
+ root[kJsonKey_Header] = header;
+ root[kJsonKey_ActualResults] = fActualResults;
+ std::string jsonStdString = root.toStyledString();
+ SkFILEWStream stream(filename);
+ stream.write(jsonStdString.c_str(), jsonStdString.length());
+ }
+
+} // namespace sk_tools
diff --git a/tools/image_expectations.h b/tools/image_expectations.h
new file mode 100644
index 0000000000..432cf6d3a8
--- /dev/null
+++ b/tools/image_expectations.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef image_expectations_DEFINED
+#define image_expectations_DEFINED
+
+#include "SkBitmap.h"
+#include "SkJSONCPP.h"
+
+namespace sk_tools {
+
+ /**
+ * Class for collecting image results (checksums) as we go.
+ */
+ class ImageResultsSummary {
+ public:
+ /**
+ * Adds this image to the summary of results.
+ *
+ * @param sourceName name of the source file that generated this result
+ * @param fileName relative path to the image output file on local disk
+ * @param hash hash to store
+ * @param tileNumber if not NULL, ptr to tile number
+ */
+ void add(const char *sourceName, const char *fileName, uint64_t hash,
+ const int *tileNumber=NULL);
+
+ /**
+ * Adds this image to the summary of results.
+ *
+ * @param sourceName name of the source file that generated this result
+ * @param fileName relative path to the image output file on local disk
+ * @param bitmap bitmap to store the hash of
+ * @param tileNumber if not NULL, ptr to tile number
+ */
+ void add(const char *sourceName, const char *fileName, const SkBitmap& bitmap,
+ const int *tileNumber=NULL);
+
+ /**
+ * Writes the summary (as constructed so far) to a file.
+ *
+ * @param filename path to write the summary to
+ */
+ void writeToFile(const char *filename);
+
+ private:
+ Json::Value fActualResults;
+ };
+
+} // namespace sk_tools
+
+#endif // image_expectations_DEFINED
diff --git a/tools/render_pictures_main.cpp b/tools/render_pictures_main.cpp
index f11eb0b80e..1bc4d21e33 100644
--- a/tools/render_pictures_main.cpp
+++ b/tools/render_pictures_main.cpp
@@ -19,6 +19,8 @@
#include "SkPictureRecorder.h"
#include "SkStream.h"
#include "SkString.h"
+
+#include "image_expectations.h"
#include "PictureRenderer.h"
#include "PictureRenderingFlags.h"
#include "picture_utils.h"