Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Unified Diff: src/utils/SkTextureCompressor.cpp

Issue 325733004: Add texture compression utility (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Some more small cleanup Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/utils/SkTextureCompressor.h ('K') | « src/utils/SkTextureCompressor.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/utils/SkTextureCompressor.cpp
diff --git a/src/utils/SkTextureCompressor.cpp b/src/utils/SkTextureCompressor.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..7696f3107a65125aabc7188239906a040985b68d
--- /dev/null
+++ b/src/utils/SkTextureCompressor.cpp
@@ -0,0 +1,167 @@
+/*
+ * 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 "SkTextureCompressor.h"
+
+#include "SkBitmap.h"
+#include "SkData.h"
+#include "SkEndian.h"
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// Utility Functions
+//
+////////////////////////////////////////////////////////////////////////////////
+
robertphillips 2014/06/09 19:50:18 Maybe SkTAbsDiff in SkTypes.h ?
krajcevski 2014/06/09 20:39:34 Done.
+template<typename T>
+static const T absolute_difference(const T& a, const T& b) {
+ return (a > b) ? (a - b) : (b - a);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// LATC compressor
+//
+////////////////////////////////////////////////////////////////////////////////
+
robertphillips 2014/06/09 19:50:18 static const int kLATCPaletteSize = 8; ? // Retur
krajcevski 2014/06/09 20:39:34 Done.
+static uint16_t compute_error(uint16_t pixel, uint8_t palette[8]) {
+ uint16_t error = 65535;
+ for (int i = 0; i < 8; ++i) {
+ uint16_t diff = absolute_difference(static_cast<uint16_t>(palette[i]), pixel);
+ error = SkMin32(diff, error);
+ }
+ return error*error;
+}
+
robertphillips 2014/06/09 19:50:18 // Maybe an overview of the compression algorithm
krajcevski 2014/06/09 20:39:34 Done.
+static uint64_t compress_latc_block(uint8_t block[16]) {
+ // Just do a simple min/max but choose from which of the
+ // two palettes is better
+ uint8_t maxVal = 0;
+ uint8_t minVal = 255;
+ for (int i = 0; i < 16; ++i) {
+ maxVal = SkMax32(maxVal, block[i]);
+ minVal = SkMin32(minVal, block[i]);
+ }
+
+ // Generate palettes
+ uint8_t palettes[2][8];
+
robertphillips 2014/06/09 19:50:18 // palette 0 is a straight up linear ramp between
krajcevski 2014/06/09 20:39:34 Done.
+ palettes[0][0] = maxVal;
+ palettes[0][1] = minVal;
+ for (int i = 1; i < 7; ++i) {
+ palettes[0][i+1] = ((7-i)*maxVal + i*minVal) / 7;
+ }
+
robertphillips 2014/06/09 19:50:18 // palette 1 is a shorter linear ramp between minV
krajcevski 2014/06/09 20:39:34 The way this algorithm is set up, probably not. In
+ palettes[1][0] = minVal;
+ palettes[1][1] = maxVal;
+ for (int i = 1; i < 5; ++i) {
+ palettes[1][i+1] = ((5-i)*maxVal + i*minVal) / 5;
+ }
+ palettes[1][6] = 0;
+ palettes[1][7] = 255;
robertphillips 2014/06/09 19:50:17 The raster code only ever generates 16 different a
krajcevski 2014/06/09 20:39:34 Yes, probably. I think that investigating better c
+
robertphillips 2014/06/09 19:50:18 error0 & error1 to match palette numbering ?
krajcevski 2014/06/09 20:39:34 Done.
+ // Figure out which of the two is better
+ uint16_t error1 = 0, error2 = 0;
+ for (int i = 0; i < 16; ++i) {
+ uint16_t pixel = static_cast<uint16_t>(block[i]);
+ error1 += compute_error(pixel, palettes[0]);
+ error2 += compute_error(pixel, palettes[1]);
+ }
+ uint8_t *palette = (error1 > error2) ? palettes[1] : palettes[0];
+
+ // Compress the chosen palette
+ uint64_t result = 0;
robertphillips 2014/06/09 19:50:18 Is this jamming the right values in?
krajcevski 2014/06/09 20:39:34 Yes, although I did have to double check.
+ result |= static_cast<uint64_t>(palette[0]);
+ result |= static_cast<uint64_t>(palette[1]) << 8;
+
+ uint64_t indices = 0;
+ for (int i = 15; i >= 0; --i) {
+ uint8_t pixel = block[i];
+
+ uint8_t minError = absolute_difference(pixel, palette[0]);
+ uint8_t minErrorIndex = 0;
robertphillips 2014/06/09 19:50:18 So we essentially do this loop to select the palet
krajcevski 2014/06/09 20:39:34 The palette is selected in the preceding loop. Thi
+ for (int j = 1; j < 8; ++j) {
+ uint8_t error = absolute_difference(pixel, palette[j]);
+ if (error < minError) {
+ minError = error;
+ minErrorIndex = j;
+ }
+ }
+ SkASSERT(minErrorIndex < 8);
+
+ indices <<= 3;
+ indices |= minErrorIndex;
+ }
+ result |= indices << 16;
+
+ // We assume everything is little endian, if it's not then make it so.
+ return SkEndian_SwapLE64(result);
+}
+
+static SkData *compress_a8_to_latc(const SkBitmap &bm) {
+ // LATC compressed texels down into square 4x4 blocks
+ static const int kLATCBlockSize = 4;
+
+ // Make sure that our data is well-formed enough to be
+ // considered for LATC compression
+ if (bm.width() == 0 || bm.height() == 0 ||
+ (bm.width() % kLATCBlockSize) != 0 ||
+ (bm.height() % kLATCBlockSize) != 0 ||
+ (bm.config() != SkBitmap::kA8_Config)) {
+ return NULL;
+ }
+
+ // The LATC format is 64 bits per 4x4 block.
+ static const int kLATCEncodedBlockSize = 8;
+
+ int blocksX = bm.width() / kLATCBlockSize;
+ int blocksY = bm.height() / kLATCBlockSize;
+
+ int compressedDataSize = blocksX * blocksY * kLATCEncodedBlockSize;
+ uint64_t* dst = reinterpret_cast<uint64_t*>(sk_malloc_throw(compressedDataSize));
+
+ uint8_t block[16];
+ const uint8_t* row = reinterpret_cast<const uint8_t*>(bm.getPixels());
+ uint64_t* encPtr = dst;
+ for (int y = 0; y < blocksY; ++y) {
+ for (int x = 0; x < blocksX; ++x) {
+ memcpy(block, row + (kLATCBlockSize * x), 4);
+ memcpy(block + 4, row + bm.rowBytes() + (kLATCBlockSize * x), 4);
+ memcpy(block + 8, row + 2*bm.rowBytes() + (kLATCBlockSize * x), 4);
+ memcpy(block + 12, row + 3*bm.rowBytes() + (kLATCBlockSize * x), 4);
+
+ *encPtr = compress_latc_block(block);
+ ++encPtr;
+ }
+ row += kLATCBlockSize * bm.rowBytes();
+ }
+
+ return SkData::NewFromMalloc(dst, compressedDataSize);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+namespace SkTextureCompressor {
+
+typedef SkData *(*CompressBitmapProc)(const SkBitmap &bitmap);
+
+SkData *CompressBitmapToConfig(const SkBitmap &bitmap, Format config) {
+ CompressBitmapProc kProcMap[SkBitmap::kConfigCount][kFormatCnt];
+ memset(kProcMap, 0, sizeof(kProcMap));
+
+ // Map available bitmap configs to compression functions
+ kProcMap[SkBitmap::kA8_Config][kLATC_Format] = compress_a8_to_latc;
+
+ CompressBitmapProc proc = kProcMap[bitmap.config()][config];
+ if (NULL != proc) {
+ return proc(bitmap);
+ }
+
+ return NULL;
+}
+
+} // namespace SkTextureCompressor
« src/utils/SkTextureCompressor.h ('K') | « src/utils/SkTextureCompressor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698