2014-05-22 18:40:29 +00:00
|
|
|
/*
|
|
|
|
* 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 "gm.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
2014-05-22 18:40:29 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-06-02 14:38:14 +00:00
|
|
|
#include "SkData.h"
|
2015-01-08 02:04:45 +00:00
|
|
|
#include "SkImageGenerator.h"
|
2014-05-22 18:40:29 +00:00
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
|
2014-06-04 16:57:47 +00:00
|
|
|
#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
|
|
|
|
|
2014-05-22 18:40:29 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/**
|
2014-06-03 20:04:35 +00:00
|
|
|
* Test decoding an image from a PKM or KTX file and then
|
2014-05-22 18:40:29 +00:00
|
|
|
* from compressed ETC1 data.
|
|
|
|
*/
|
|
|
|
class ETC1BitmapGM : public GM {
|
|
|
|
public:
|
|
|
|
ETC1BitmapGM() { }
|
|
|
|
virtual ~ETC1BitmapGM() { }
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-06-03 20:04:35 +00:00
|
|
|
SkString str = SkString("etc1bitmap_");
|
|
|
|
str.append(this->fileExtension());
|
|
|
|
return str;
|
2014-05-22 18:40:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(128, 128);
|
2014-05-22 18:40:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 20:04:35 +00:00
|
|
|
virtual SkString fileExtension() const = 0;
|
2014-05-22 18:40:29 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-05-22 18:40:29 +00:00
|
|
|
SkBitmap bm;
|
2014-07-01 19:35:49 +00:00
|
|
|
SkString filename = GetResourcePath("mandrill_128.");
|
2014-06-03 20:04:35 +00:00
|
|
|
filename.append(this->fileExtension());
|
2014-06-03 16:04:33 +00:00
|
|
|
SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str()));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fileData) {
|
2014-06-02 14:38:14 +00:00
|
|
|
SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
|
2014-05-22 18:40:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-06-02 14:38:14 +00:00
|
|
|
|
2015-09-14 15:52:12 +00:00
|
|
|
SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(fileData));
|
|
|
|
canvas->drawImage(image, 0, 0);
|
2014-05-22 18:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-06-03 20:04:35 +00:00
|
|
|
// This class specializes ETC1BitmapGM to load the mandrill_128.pkm file.
|
|
|
|
class ETC1Bitmap_PKM_GM : public ETC1BitmapGM {
|
|
|
|
public:
|
|
|
|
ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { }
|
|
|
|
virtual ~ETC1Bitmap_PKM_GM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString fileExtension() const override { return SkString("pkm"); }
|
2014-06-03 20:04:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef ETC1BitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class specializes ETC1BitmapGM to load the mandrill_128.ktx file.
|
|
|
|
class ETC1Bitmap_KTX_GM : public ETC1BitmapGM {
|
|
|
|
public:
|
|
|
|
ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { }
|
|
|
|
virtual ~ETC1Bitmap_KTX_GM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString fileExtension() const override { return SkString("ktx"); }
|
2014-06-03 20:04:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef ETC1BitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-08-08 18:38:27 +00:00
|
|
|
// This class specializes ETC1BitmapGM to load the mandrill_128.r11.ktx file.
|
|
|
|
class ETC1Bitmap_R11_KTX_GM : public ETC1BitmapGM {
|
|
|
|
public:
|
|
|
|
ETC1Bitmap_R11_KTX_GM() : ETC1BitmapGM() { }
|
|
|
|
virtual ~ETC1Bitmap_R11_KTX_GM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString fileExtension() const override { return SkString("r11.ktx"); }
|
2014-08-08 18:38:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef ETC1BitmapGM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-06-04 16:57:47 +00:00
|
|
|
#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:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-06-04 16:57:47 +00:00
|
|
|
return SkString("etc1bitmap_npot");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(124, 124);
|
2014-06-04 16:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-06-04 16:57:47 +00:00
|
|
|
SkBitmap bm;
|
2014-07-01 19:35:49 +00:00
|
|
|
SkString pkmFilename = GetResourcePath("mandrill_128.pkm");
|
|
|
|
SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str()));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fileData) {
|
2014-06-04 16:57:47 +00:00
|
|
|
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));
|
|
|
|
|
2015-09-14 15:52:12 +00:00
|
|
|
SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(nonPOTData));
|
|
|
|
canvas->drawImage(image, 0, 0);
|
2014-06-04 16:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
#endif // SK_IGNORE_ETC1_SUPPORT
|
|
|
|
|
2014-05-22 18:40:29 +00:00
|
|
|
} // namespace skiagm
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new skiagm::ETC1Bitmap_PKM_GM;)
|
|
|
|
DEF_GM(return new skiagm::ETC1Bitmap_KTX_GM;)
|
|
|
|
DEF_GM(return new skiagm::ETC1Bitmap_R11_KTX_GM;)
|
2014-06-04 16:57:47 +00:00
|
|
|
|
|
|
|
#ifndef SK_IGNORE_ETC1_SUPPORT
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new skiagm::ETC1Bitmap_NPOT_GM;)
|
2014-06-04 16:57:47 +00:00
|
|
|
#endif // SK_IGNORE_ETC1_SUPPORT
|