Add support for all compressed formats in KTX file format
authorkrajcevski <krajcevski@google.com>
Tue, 5 Aug 2014 21:13:36 +0000 (14:13 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 5 Aug 2014 21:13:36 +0000 (14:13 -0700)
R=robertphillips@google.com

Author: krajcevski@google.com

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

gyp/ktx.gyp
src/gpu/SkGr.cpp
src/images/SkImageDecoder_ktx.cpp
src/utils/SkTextureCompressor.h
third_party/ktx/ktx.cpp
third_party/ktx/ktx.h

index 141eba1fd1857bed084deb976dd9bfbb7b4e1fec..32a5c32ce855ea44ed4739fd15b677eb74bf2684 100644 (file)
@@ -8,7 +8,9 @@
     'type': 'static_library',
     'include_dirs' : [
       '../third_party/ktx',
-      '../src/gpu'
+      '../include/gpu',
+      '../src/gpu',
+      '../src/utils',
     ],
     'sources': [
       '../third_party/ktx/ktx.cpp',
index 5c0464da20acd72ef60f4569b62fe679c6c619e3..0a45100521232f1ac26dca6b5cac96a1ec6277ff 100644 (file)
@@ -11,6 +11,7 @@
 #include "SkData.h"
 #include "SkMessageBus.h"
 #include "SkPixelRef.h"
+#include "SkTextureCompressor.h"
 #include "GrResourceCache.h"
 #include "GrGpu.h"
 #include "effects/GrDitherEffect.h"
@@ -165,7 +166,7 @@ static GrTexture *load_etc1_texture(GrContext* ctx,
         SkKTXFile ktx(data);
 
         // Is it actually an ETC1 texture?
-        if (!ktx.isETC1()) {
+        if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
             return NULL;
         }
 
index 058ab7264695cb8c6cfb7274f2d0a87646850ee6..c06450c6315bf08df8c20a9967500f58623c60de 100644 (file)
@@ -105,7 +105,7 @@ bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
     // Lock the pixels, since we're about to write to them...
     SkAutoLockPixels alp(*bm);
 
-    if (ktxFile.isETC1()) {
+    if (ktxFile.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
         if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
             return false;
         }
@@ -113,11 +113,12 @@ bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
         // ETC1 Data is encoded as RGB pixels, so we should extract it as such
         int nPixels = width * height;
         SkAutoMalloc outRGBData(nPixels * 3);
-        etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get());
+        uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get());
 
         // Decode ETC1
-        const etc1_byte *buf = reinterpret_cast<const etc1_byte *>(ktxFile.pixelData());
-        if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
+        const uint8_t *buf = reinterpret_cast<const uint8_t *>(ktxFile.pixelData());
+        if (!SkTextureCompressor::DecompressBufferFromFormat(
+                outRGBDataPtr, width*3, buf, width, height, SkTextureCompressor::kETC1_Format)) {
             return false;
         }
 
index 4254ae76fb902272eaabfae59d7940bb3499529e..e44e7b353dcd13c3919b1de3f89019df8d0aaf05 100644 (file)
@@ -9,9 +9,9 @@
 #define SkTextureCompressor_DEFINED
 
 #include "SkImageInfo.h"
-#include "SkBlitter.h"
 
 class SkBitmap;
+class SkBlitter;
 class SkData;
 
 namespace SkTextureCompressor {
index a05498b7e80b27ee4fc1f19263910976258599a7..ebcc5eb18747b267caba91bdaaaeb9424fe0a023 100644 (file)
 #include "SkEndian.h"
 
 #include "gl/GrGLDefines.h"
+#include "GrConfig.h"
 
 #include "etc1.h"
 
+static inline uint32_t compressed_fmt_to_gl_define(SkTextureCompressor::Format fmt) {
+    static const uint32_t kGLDefineMap[SkTextureCompressor::kFormatCnt] = {
+        GR_GL_COMPRESSED_LUMINANCE_LATC1,  // kLATC_Format
+        GR_GL_COMPRESSED_R11,  // kR11_EAC_Format
+        GR_GL_COMPRESSED_RGB8_ETC1,  // kETC1_Format
+        GR_GL_COMPRESSED_RGBA_ASTC_12x12,  // kASTC_12x12_Format
+    };
+
+    GR_STATIC_ASSERT(0 == SkTextureCompressor::kLATC_Format);
+    GR_STATIC_ASSERT(1 == SkTextureCompressor::kR11_EAC_Format);
+    GR_STATIC_ASSERT(2 == SkTextureCompressor::kETC1_Format);
+    GR_STATIC_ASSERT(3 == SkTextureCompressor::kASTC_12x12_Format);
+    GR_STATIC_ASSERT(SK_ARRAY_COUNT(kGLDefineMap) == SkTextureCompressor::kFormatCnt);
+
+    return kGLDefineMap[fmt];
+}
+
 #define KTX_FILE_IDENTIFIER_SIZE 12
 static const uint8_t KTX_FILE_IDENTIFIER[KTX_FILE_IDENTIFIER_SIZE] = {
     0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
@@ -123,8 +141,19 @@ SkString SkKTXFile::getValueForKey(const SkString& key) const {
     return SkString();
 }
 
-bool SkKTXFile::isETC1() const {
-    return this->valid() && GR_GL_COMPRESSED_RGB8_ETC1 == fHeader.fGLInternalFormat;
+bool SkKTXFile::isCompressedFormat(SkTextureCompressor::Format fmt) const {
+    if (!this->valid()) {
+        return false;
+    }
+
+    // This has many aliases
+    bool isFmt = false;
+    if (fmt == SkTextureCompressor::kLATC_Format) {
+        isFmt = GR_GL_COMPRESSED_RED_RGTC1 == fHeader.fGLInternalFormat ||
+                GR_GL_COMPRESSED_3DC_X == fHeader.fGLInternalFormat;
+    }
+
+    return isFmt || compressed_fmt_to_gl_define(fmt) == fHeader.fGLInternalFormat;
 }
 
 bool SkKTXFile::isRGBA8() const {
index 2f445a817a0e6a9fcf0fe10ffc2560370e8f65e7..1114b49bf94a4a1b396db03db5b8a73cf821797e 100644 (file)
@@ -11,6 +11,7 @@
 #define SkKTXFile_DEFINED
 
 #include "SkData.h"
+#include "SkTextureCompressor.h"
 #include "SkTypes.h"
 #include "SkTDArray.h"
 #include "SkString.h"
@@ -58,7 +59,7 @@ public:
 
     int numMipmaps() const { return static_cast<int>(fHeader.fNumberOfMipmapLevels); }
 
-    bool isETC1() const;
+    bool isCompressedFormat(SkTextureCompressor::Format fmt) const;
     bool isRGBA8() const;
     bool isRGB8() const;