blob: 503605b003e4303cb42b168fe9ae0175f16f208e [file] [log] [blame]
krajcevski2b310e42014-06-11 12:26:49 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBitmap.h"
9#include "SkData.h"
10#include "SkEndian.h"
11#include "SkImageInfo.h"
12#include "SkTextureCompressor.h"
13#include "Test.h"
14
krajcevski2b310e42014-06-11 12:26:49 -070015/**
16 * Make sure that we properly fail when we don't have multiple of four image dimensions.
17 */
krajcevskib5294e82014-07-30 08:34:51 -070018DEF_TEST(CompressAlphaFailDimensions, reporter) {
krajcevski2b310e42014-06-11 12:26:49 -070019 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070020 static const int kWidth = 17;
21 static const int kHeight = 17;
krajcevski2b310e42014-06-11 12:26:49 -070022 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
krajcevskib5294e82014-07-30 08:34:51 -070023
24 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that we
25 // are violating those assumptions. And if we are, then we're also violating the
26 // assumptions of ASTC, which is 12x12 since any number not divisible by 4 is
27 // also not divisible by 12. Our dimensions are prime, so any block dimension
28 // larger than 1 should fail.
29 REPORTER_ASSERT(reporter, kWidth % 4 != 0);
30 REPORTER_ASSERT(reporter, kHeight % 4 != 0);
krajcevski2b310e42014-06-11 12:26:49 -070031
32 bool setInfoSuccess = bitmap.setInfo(info);
33 REPORTER_ASSERT(reporter, setInfoSuccess);
34
35 bool allocPixelsSuccess = bitmap.allocPixels(info);
36 REPORTER_ASSERT(reporter, allocPixelsSuccess);
37 bitmap.unlockPixels();
krajcevskib5294e82014-07-30 08:34:51 -070038
39 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
40 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
41 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
42 REPORTER_ASSERT(reporter, NULL == data);
43 }
krajcevski2b310e42014-06-11 12:26:49 -070044}
45
46/**
47 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070048 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070049 */
krajcevskib5294e82014-07-30 08:34:51 -070050DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevski2b310e42014-06-11 12:26:49 -070051 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070052 static const int kWidth = 12;
53 static const int kHeight = 12;
krajcevski2b310e42014-06-11 12:26:49 -070054 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
krajcevskib5294e82014-07-30 08:34:51 -070055
56 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
57 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
58 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
59 // need to be updated.
60 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
61 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070062
63 bool setInfoSuccess = bitmap.setInfo(info);
64 REPORTER_ASSERT(reporter, setInfoSuccess);
65
66 bool allocPixelsSuccess = bitmap.allocPixels(info);
67 REPORTER_ASSERT(reporter, allocPixelsSuccess);
68 bitmap.unlockPixels();
69
krajcevskib5294e82014-07-30 08:34:51 -070070 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
71 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
72 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
73 REPORTER_ASSERT(reporter, NULL == data);
74 }
krajcevski2b310e42014-06-11 12:26:49 -070075}
76
77/**
78 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
79 */
80DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070081
82 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
83 static const int kLATCEncodedBlockSize = 8;
84
krajcevski2b310e42014-06-11 12:26:49 -070085 SkBitmap bitmap;
86 static const int kWidth = 8;
87 static const int kHeight = 8;
88 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
89
90 bool setInfoSuccess = bitmap.setInfo(info);
91 REPORTER_ASSERT(reporter, setInfoSuccess);
92
93 bool allocPixelsSuccess = bitmap.allocPixels(info);
94 REPORTER_ASSERT(reporter, allocPixelsSuccess);
95 bitmap.unlockPixels();
96
krajcevskib5294e82014-07-30 08:34:51 -070097 int latcDimX, latcDimY;
98 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
99
100 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
101 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
102 const size_t kSizeToBe =
103 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
104 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
105 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700106
107 for (int lum = 0; lum < 256; ++lum) {
108 bitmap.lockPixels();
109 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
110 REPORTER_ASSERT(reporter, NULL != pixels);
111
112 for (int i = 0; i < kWidth*kHeight; ++i) {
113 pixels[i] = lum;
114 }
115 bitmap.unlockPixels();
116
krajcevski2b310e42014-06-11 12:26:49 -0700117 SkAutoDataUnref latcData(
118 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat));
119 REPORTER_ASSERT(reporter, NULL != latcData);
120 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
121
krajcevskib5294e82014-07-30 08:34:51 -0700122 // Make sure that it all matches a given block encoding. Since we have
123 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
124 // an approximation scheme that optimizes for speed against coverage maps.
125 // That means that each palette in the encoded block is exactly the same,
126 // and that the three bits saved per pixel are computed from the top three
127 // bits of the luminance value.
128 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
129 const uint64_t kIndex = kIndexEncodingMap[lum >> 5];
130 const uint64_t kConstColorEncoding =
131 SkEndian_SwapLE64(
132 255 |
133 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) |
134 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) |
135 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) |
136 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61));
137
krajcevski2b310e42014-06-11 12:26:49 -0700138 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
krajcevskib5294e82014-07-30 08:34:51 -0700139 for (size_t i = 0; i < (kSizeToBe/8); ++i) {
krajcevski2b310e42014-06-11 12:26:49 -0700140 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
141 }
142 }
143}