Fix CodecSubset benches seg faults for kIndex8
authormsarett <msarett@google.com>
Tue, 30 Jun 2015 20:29:37 +0000 (13:29 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 30 Jun 2015 20:29:37 +0000 (13:29 -0700)
All of the CodecSubset benches fail when the color type is
kIndex8.  We need to pass a color table to allocPixels()
when we want to decode to kIndex8 or it will throw a failure.

BUG=skia:

Review URL: https://codereview.chromium.org/1213983003

bench/subset/SubsetBenchPriv.h
bench/subset/SubsetSingleBench.cpp
bench/subset/SubsetTranslateBench.cpp
bench/subset/SubsetZoomBench.cpp

index e0eb2ff..d2c3410 100644 (file)
@@ -32,4 +32,19 @@ static const char* get_color_name(SkColorType colorType) {
     }
 }
 
+/*
+ * If we plan to decode to kIndex8, we must create a color table and pass it to the
+ * bitmap when we allocate pixels.  Otherwise, we simply allocate pixels using the
+ * decode info.
+ */
+static inline void alloc_pixels(SkBitmap* bitmap, const SkImageInfo& info, SkPMColor* colors,
+        int colorCount) {
+    if (kIndex_8_SkColorType == info.colorType()) {
+        SkAutoTUnref<SkColorTable> colorTable(SkNEW_ARGS(SkColorTable, (colors, colorCount)));
+        bitmap->allocPixels(info, NULL, colorTable);
+    } else {
+        bitmap->allocPixels(info);
+    }
+}
+
 #endif // SubsetBenchPriv_DEFINED
index 68c94cd..6c1da5c 100644 (file)
@@ -70,7 +70,8 @@ void SubsetSingleBench::onDraw(const int n, SkCanvas* canvas) {
                     info, NULL, colors, &colorCount);
 
             SkBitmap bitmap;
-            bitmap.allocPixels(info.makeWH(fSubsetWidth, fSubsetHeight));
+            SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight);
+            alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
 
             scanlineDecoder->skipScanlines(fOffsetTop);
             uint32_t bpp = info.bytesPerPixel();
index 620b6f4..0769015 100644 (file)
@@ -68,7 +68,8 @@ void SubsetTranslateBench::onDraw(const int n, SkCanvas* canvas) {
             SkBitmap bitmap;
             // Note that we use the same bitmap for all of the subsets.
             // It might be larger than necessary for the end subsets.
-            bitmap.allocPixels(info.makeWH(fSubsetWidth, fSubsetHeight));
+            SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight);
+            alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
 
             for (int x = 0; x < info.width(); x += fSubsetWidth) {
                 for (int y = 0; y < info.height(); y += fSubsetHeight) {
index 3ce193b..39f234a 100644 (file)
@@ -77,7 +77,8 @@ void SubsetZoomBench::onDraw(const int n, SkCanvas* canvas) {
                 // Note that if we subsetted and scaled in a single step, we could use the
                 // same bitmap - as is often done in actual use cases.
                 SkBitmap bitmap;
-                bitmap.allocPixels(info.makeWH(subsetWidth, subsetHeight));
+                SkImageInfo subsetInfo = info.makeWH(subsetWidth, subsetHeight);
+                alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
 
                 uint32_t bpp = info.bytesPerPixel();
                 scanlineDecoder->skipScanlines(subsetStartY);