2014-06-05 14:03:39 +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.
|
|
|
|
*/
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "Resources.h"
|
2014-06-05 14:03:39 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkData.h"
|
2015-01-08 02:04:45 +00:00
|
|
|
#include "SkImageGenerator.h"
|
2014-06-05 14:03:39 +00:00
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPixelRef.h"
|
|
|
|
|
|
|
|
#ifndef SK_IGNORE_ETC1_SUPPORT
|
|
|
|
|
|
|
|
#include "etc1.h"
|
|
|
|
|
|
|
|
// This takes the etc1 data pointed to by orig, and copies it `factor` times in each
|
2015-08-27 14:41:13 +00:00
|
|
|
// dimension. The return value is the new data or nullptr on error.
|
2014-06-05 14:03:39 +00:00
|
|
|
static etc1_byte* create_expanded_etc1_bitmap(const uint8_t* orig, int factor) {
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(orig);
|
2014-06-05 14:03:39 +00:00
|
|
|
SkASSERT(factor > 1);
|
|
|
|
|
|
|
|
const etc1_byte* origData = reinterpret_cast<const etc1_byte*>(orig);
|
|
|
|
if (!etc1_pkm_is_valid(orig)) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
etc1_uint32 origWidth = etc1_pkm_get_width(origData);
|
|
|
|
etc1_uint32 origHeight = etc1_pkm_get_height(origData);
|
|
|
|
|
|
|
|
// The width and height must be aligned along block boundaries
|
|
|
|
static const etc1_uint32 kETC1BlockWidth = 4;
|
|
|
|
static const etc1_uint32 kETC1BlockHeight = 4;
|
|
|
|
if ((origWidth % kETC1BlockWidth) != 0 ||
|
|
|
|
(origHeight % kETC1BlockHeight) != 0) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The picture must be at least as large as a block.
|
|
|
|
if (origWidth <= kETC1BlockWidth || origHeight <= kETC1BlockHeight) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
etc1_uint32 newWidth = origWidth * factor;
|
|
|
|
etc1_uint32 newHeight = origHeight * factor;
|
|
|
|
|
|
|
|
etc1_uint32 newDataSz = etc1_get_encoded_data_size(newWidth, newHeight);
|
|
|
|
etc1_byte* newData = reinterpret_cast<etc1_byte *>(
|
|
|
|
sk_malloc_throw(newDataSz + ETC_PKM_HEADER_SIZE));
|
|
|
|
etc1_pkm_format_header(newData, newWidth, newHeight);
|
|
|
|
|
|
|
|
etc1_byte* copyInto = newData;
|
|
|
|
|
|
|
|
copyInto += ETC_PKM_HEADER_SIZE;
|
|
|
|
origData += ETC_PKM_HEADER_SIZE;
|
|
|
|
|
|
|
|
etc1_uint32 origBlocksX = (origWidth >> 2);
|
|
|
|
etc1_uint32 origBlocksY = (origHeight >> 2);
|
|
|
|
etc1_uint32 newBlocksY = (newHeight >> 2);
|
|
|
|
etc1_uint32 origRowSzInBytes = origBlocksX * ETC1_ENCODED_BLOCK_SIZE;
|
|
|
|
|
|
|
|
for (etc1_uint32 j = 0; j < newBlocksY; ++j) {
|
|
|
|
const etc1_byte* rowStart = origData + ((j % origBlocksY) * origRowSzInBytes);
|
|
|
|
for(etc1_uint32 i = 0; i < newWidth; i += origWidth) {
|
|
|
|
memcpy(copyInto, rowStart, origRowSzInBytes);
|
|
|
|
copyInto += origRowSzInBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the base class for all of the benches in this file. In general
|
|
|
|
// the ETC1 benches should all be working on the same data. Due to the
|
|
|
|
// simplicity of the PKM file, that data is the 128x128 mandrill etc1
|
|
|
|
// compressed texture repeated by some factor (currently 8 -> 1024x1024)
|
2014-06-19 19:32:29 +00:00
|
|
|
class ETCBitmapBenchBase : public Benchmark {
|
2014-06-05 14:03:39 +00:00
|
|
|
public:
|
|
|
|
ETCBitmapBenchBase() : fPKMData(loadPKM()) {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fPKMData) {
|
2014-06-05 14:03:39 +00:00
|
|
|
SkDebugf("Could not load PKM data!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkAutoDataUnref fPKMData;
|
|
|
|
|
|
|
|
private:
|
2014-07-01 19:35:49 +00:00
|
|
|
SkData* loadPKM() {
|
|
|
|
SkString pkmFilename = GetResourcePath("mandrill_128.pkm");
|
2014-06-05 14:03:39 +00:00
|
|
|
// Expand the data
|
2014-07-01 19:35:49 +00:00
|
|
|
SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str()));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fileData) {
|
2014-06-05 14:03:39 +00:00
|
|
|
SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const etc1_uint32 kExpansionFactor = 8;
|
|
|
|
etc1_byte* expandedETC1 =
|
|
|
|
create_expanded_etc1_bitmap(fileData->bytes(), kExpansionFactor);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == expandedETC1) {
|
2014-06-05 14:03:39 +00:00
|
|
|
SkDebugf("Error expanding ETC1 data by factor of %d\n", kExpansionFactor);
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
etc1_uint32 width = etc1_pkm_get_width(expandedETC1);
|
|
|
|
etc1_uint32 height = etc1_pkm_get_width(expandedETC1);
|
|
|
|
etc1_uint32 dataSz = ETC_PKM_HEADER_SIZE + etc1_get_encoded_data_size(width, height);
|
|
|
|
return SkData::NewFromMalloc(expandedETC1, dataSz);
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2014-06-05 14:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is the rendering benchmark. Prior to rendering the data, create a
|
|
|
|
// bitmap using the etc1 data.
|
|
|
|
class ETCBitmapBench : public ETCBitmapBenchBase {
|
|
|
|
public:
|
|
|
|
ETCBitmapBench(bool decompress, Backend backend)
|
|
|
|
: fDecompress(decompress), fBackend(backend) { }
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2014-06-05 14:03:39 +00:00
|
|
|
return backend == this->fBackend;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2014-06-05 14:03:39 +00:00
|
|
|
if (kGPU_Backend == this->fBackend) {
|
|
|
|
if (this->fDecompress) {
|
|
|
|
return "etc1bitmap_render_gpu_decompressed";
|
|
|
|
} else {
|
|
|
|
return "etc1bitmap_render_gpu_compressed";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SkASSERT(kRaster_Backend == this->fBackend);
|
|
|
|
if (this->fDecompress) {
|
|
|
|
return "etc1bitmap_render_raster_decompressed";
|
|
|
|
} else {
|
|
|
|
return "etc1bitmap_render_raster_compressed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fPKMData) {
|
2014-06-05 14:03:39 +00:00
|
|
|
SkDebugf("Failed to load PKM data!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:52:12 +00:00
|
|
|
if (fDecompress) {
|
|
|
|
SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(fPKMData));
|
|
|
|
gen->generateBitmap(&fBitmap);
|
|
|
|
} else {
|
|
|
|
fImage.reset(SkImage::NewFromEncoded(fPKMData));
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2014-06-05 14:03:39 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2015-09-14 15:52:12 +00:00
|
|
|
if (fDecompress) {
|
|
|
|
canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
|
|
|
|
} else {
|
|
|
|
canvas->drawImage(fImage, 0, 0, nullptr);
|
|
|
|
}
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkBitmap fBitmap;
|
2015-09-14 15:52:12 +00:00
|
|
|
SkAutoTUnref<SkImage> fImage;
|
|
|
|
|
2014-06-05 14:03:39 +00:00
|
|
|
bool decompress() const { return fDecompress; }
|
|
|
|
Backend backend() const { return fBackend; }
|
|
|
|
private:
|
|
|
|
const bool fDecompress;
|
|
|
|
const Backend fBackend;
|
|
|
|
typedef ETCBitmapBenchBase INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This benchmark is identical to the previous benchmark, but it explicitly forces
|
|
|
|
// an upload to the GPU before each draw call. We do this by notifying the bitmap
|
|
|
|
// that the pixels have changed (even though they haven't).
|
|
|
|
class ETCBitmapUploadBench : public ETCBitmapBench {
|
|
|
|
public:
|
|
|
|
ETCBitmapUploadBench(bool decompress, Backend backend)
|
|
|
|
: ETCBitmapBench(decompress, backend) { }
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2014-06-05 14:03:39 +00:00
|
|
|
if (kGPU_Backend == this->backend()) {
|
|
|
|
if (this->decompress()) {
|
|
|
|
return "etc1bitmap_upload_gpu_decompressed";
|
|
|
|
} else {
|
|
|
|
return "etc1bitmap_upload_gpu_compressed";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SkASSERT(kRaster_Backend == this->backend());
|
|
|
|
if (this->decompress()) {
|
|
|
|
return "etc1bitmap_upload_raster_decompressed";
|
|
|
|
} else {
|
|
|
|
return "etc1bitmap_upload_raster_compressed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2014-06-20 18:33:59 +00:00
|
|
|
SkPixelRef* pr = fBitmap.pixelRef();
|
2014-06-05 14:03:39 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2014-06-20 18:33:59 +00:00
|
|
|
if (pr) {
|
|
|
|
pr->notifyPixelsChanged();
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
|
2014-06-05 14:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef ETCBitmapBench INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kRaster_Backend);)
|
|
|
|
DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kRaster_Backend);)
|
2014-06-05 14:03:39 +00:00
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kGPU_Backend);)
|
|
|
|
DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kGPU_Backend);)
|
2014-06-05 14:03:39 +00:00
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kRaster_Backend);)
|
|
|
|
DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kRaster_Backend);)
|
2014-06-05 14:03:39 +00:00
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kGPU_Backend);)
|
|
|
|
DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kGPU_Backend);)
|
2014-06-05 14:03:39 +00:00
|
|
|
|
|
|
|
#endif // SK_IGNORE_ETC1_SUPPORT
|