Add non power of two gm for ETC1 bitmap

R=robertphillips@google.com

Author: krajcevski@google.com

Review URL: https://codereview.chromium.org/316813005
diff --git a/gm/etc1bitmap.cpp b/gm/etc1bitmap.cpp
index ec71d8d..cdf8617 100644
--- a/gm/etc1bitmap.cpp
+++ b/gm/etc1bitmap.cpp
@@ -12,6 +12,61 @@
 #include "SkImageDecoder.h"
 #include "SkOSFile.h"
 
+#ifndef SK_IGNORE_ETC1_SUPPORT
+
+#include "etc1.h"
+
+/**
+ *  Remove the last row and column of ETC1 blocks, effectively
+ *  making a texture that started as power of two into a texture
+ *  that is no longer power of two...
+ */
+bool slice_etc1_data(void *data, int* width, int* height) {
+
+    // First, parse the data and get to it...
+    etc1_byte *origData = reinterpret_cast<etc1_byte *>(data);
+    if (!etc1_pkm_is_valid(origData)) {
+        return false;
+    }
+
+    int origW = etc1_pkm_get_width(origData);
+    int origH = etc1_pkm_get_height(origData);
+
+    int blockWidth = (origW + 3) >> 2;
+    int blockHeight = (origH + 3) >> 2;
+
+    // Make sure that we have blocks to trim off..
+    if (blockWidth < 2 || blockHeight < 2) {
+        return false;
+    }
+
+    int newWidth = (blockWidth - 1) << 2;
+    int newHeight = (blockHeight - 1) << 2;
+
+    size_t newDataSz = etc1_get_encoded_data_size(newWidth, newHeight) + ETC_PKM_HEADER_SIZE;
+    SkAutoMalloc am(newDataSz);
+
+    etc1_byte *newData = reinterpret_cast<etc1_byte *>(am.get());
+
+    etc1_pkm_format_header(newData, newWidth, newHeight);
+    newData += ETC_PKM_HEADER_SIZE;
+    origData += ETC_PKM_HEADER_SIZE;
+
+    for (int j = 0; j < blockHeight - 1; ++j) {
+        memcpy(newData, origData, (blockWidth - 1)*ETC1_ENCODED_BLOCK_SIZE);
+        origData += blockWidth*ETC1_ENCODED_BLOCK_SIZE;
+        newData += (blockWidth - 1)*ETC1_ENCODED_BLOCK_SIZE;
+    }
+
+    // Stick the data back whence it came
+    memcpy(data, am.get(), newDataSz);
+    *width = newWidth;
+    *height = newHeight;
+
+    return true;
+}
+#endif  // SK_IGNORE_ETC1_SUPPORT
+
 namespace skiagm {
 
 /**
@@ -90,9 +145,76 @@
     typedef ETC1BitmapGM INHERITED;
 };
 
+#ifndef SK_IGNORE_ETC1_SUPPORT
+/**
+ *  Test decoding an image from a PKM file and then
+ *  from non-power-of-two compressed ETC1 data. First slice
+ *  off a row and column of blocks in order to make it non-power
+ *  of two.
+ */
+class ETC1Bitmap_NPOT_GM : public GM {
+public:
+    ETC1Bitmap_NPOT_GM() { }
+    virtual ~ETC1Bitmap_NPOT_GM() { }
+
+protected:
+    virtual SkString onShortName() SK_OVERRIDE {
+        return SkString("etc1bitmap_npot");
+    }
+
+    virtual SkISize onISize() SK_OVERRIDE {
+        return make_isize(124, 124);
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+
+        SkBitmap bm;
+        SkString filename = SkOSPath::SkPathJoin(
+                INHERITED::gResourcePath.c_str(), "mandrill_128.pkm");
+
+        SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str()));
+        if (NULL == fileData) {
+            SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
+            return;
+        }
+
+        SkAutoMalloc am(fileData->size());
+        memcpy(am.get(), fileData->data(), fileData->size());
+
+        int width, height;
+        if (!slice_etc1_data(am.get(), &width, &height)) {
+            SkDebugf("ETC1 Data is poorly formatted.\n");
+            return;
+        }
+
+        SkASSERT(124 == width);
+        SkASSERT(124 == height);
+
+        size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE;
+        SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz));
+
+        if (!SkInstallDiscardablePixelRef(
+                SkDecodingImageGenerator::Create(
+                    nonPOTData, SkDecodingImageGenerator::Options()), &bm)) {
+            SkDebugf("Could not install discardable pixel ref.\n");
+            return;
+        }
+
+        canvas->drawBitmap(bm, 0, 0);
+    }
+
+private:
+    typedef GM INHERITED;
+};
+#endif  // SK_IGNORE_ETC1_SUPPORT
+
 }  // namespace skiagm
 
 //////////////////////////////////////////////////////////////////////////////
 
 DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_GM); )
 DEF_GM( return SkNEW(skiagm::ETC1Bitmap_KTX_GM); )
+
+#ifndef SK_IGNORE_ETC1_SUPPORT
+DEF_GM( return SkNEW(skiagm::ETC1Bitmap_NPOT_GM); )
+#endif  // SK_IGNORE_ETC1_SUPPORT
diff --git a/gyp/SampleApp.gyp b/gyp/SampleApp.gyp
index e0579ac..b15fd2d 100644
--- a/gyp/SampleApp.gyp
+++ b/gyp/SampleApp.gyp
@@ -149,6 +149,7 @@
         'pdf.gyp:pdf',
         'views_animated.gyp:views_animated',
         'lua.gyp:lua',
+        'etc1.gyp:libetc1',
       ],
      'conditions' : [
        [ 'sample_pdf_file_viewer == 1', {
diff --git a/gyp/bench.gyp b/gyp/bench.gyp
index f85614d..8c81991 100644
--- a/gyp/bench.gyp
+++ b/gyp/bench.gyp
@@ -13,6 +13,7 @@
         'bench_timer',
         'flags.gyp:flags',
         'jsoncpp.gyp:jsoncpp',
+        'etc1.gyp:libetc1',
       ],
       'sources': [
         '../bench/ResultsWriter.cpp',
diff --git a/gyp/dm.gyp b/gyp/dm.gyp
index 7f9c42f..26d14db 100644
--- a/gyp/dm.gyp
+++ b/gyp/dm.gyp
@@ -60,6 +60,7 @@
             'jsoncpp.gyp:jsoncpp',
             'gputest.gyp:skgputest',
             'record.gyp:*',
+            'etc1.gyp:libetc1',
         ],
         'conditions': [
           ['skia_android_framework', {
diff --git a/gyp/gm.gyp b/gyp/gm.gyp
index 445022a..ee52b91 100644
--- a/gyp/gm.gyp
+++ b/gyp/gm.gyp
@@ -50,6 +50,7 @@
         'gm.gyp:gm_expectations',
         'jsoncpp.gyp:jsoncpp',
         'pdf.gyp:pdf',
+        'etc1.gyp:libetc1',
       ],
       'conditions': [
         ['skia_android_framework', {