Chromium Code Reviews| Index: bench/ETCBitmapBench.cpp |
| diff --git a/bench/ETCBitmapBench.cpp b/bench/ETCBitmapBench.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aa7d618c75564d6beb0731156773547ea4e145b5 |
| --- /dev/null |
| +++ b/bench/ETCBitmapBench.cpp |
| @@ -0,0 +1,206 @@ |
| +/* |
|
robertphillips
2014/06/04 18:53:36
2014 ?
krajcevski
2014/06/04 19:26:53
Done.
|
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkBenchmark.h" |
|
robertphillips
2014/06/04 18:53:36
Do we need SkBitmapDevice.h ?
krajcevski
2014/06/04 19:26:53
Nope. Removed.
|
| +#include "SkBitmapDevice.h" |
| +#include "SkCanvas.h" |
| +#include "SkData.h" |
| +#include "SkDecodingImageGenerator.h" |
| +#include "SkImageDecoder.h" |
| +#include "SkOSFile.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 |
| +// dimension. The return value is the new data or NULL on error. |
| +static etc1_byte* create_expanded_etc1_bitmap(const uint8_t* orig, int factor) { |
| + SkASSERT(NULL != orig); |
| + SkASSERT(factor > 1); |
| + |
| + const etc1_byte* origData = reinterpret_cast<const etc1_byte*>(orig); |
| + if (!etc1_pkm_is_valid(orig)) { |
| + return NULL; |
| + } |
| + |
| + 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 |
| + if ((origWidth & 3) != 0 || (origHeight & 3) != 0) { |
| + return NULL; |
| + } |
| + |
| + // The picture must be at least as large as a block. |
|
robertphillips
2014/06/04 18:53:36
Is the block width & height already #defined somew
krajcevski
2014/06/04 19:26:53
No, but "everyone knows" etc1 data is 4x4 pixel bl
robertphillips
2014/06/04 19:46:37
Probably just put a "static const int kETC1BlockSi
krajcevski
2014/06/04 19:57:16
Done.
|
| + if (origWidth <= 4 || origHeight <= 4) { |
| + return NULL; |
| + } |
| + |
| + 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) { |
| + etc1_uint32 row = j % origBlocksY; |
| + for(etc1_uint32 i = 0; i < newWidth; i += origWidth) { |
|
robertphillips
2014/06/04 18:53:36
rename rowStart to be srcRow and move it out of th
krajcevski
2014/06/04 19:26:53
Done.
|
| + const etc1_byte* rowStart = origData + (row * origRowSzInBytes); |
| + 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) |
| +class ETCBitmapBenchBase : public SkBenchmark { |
| +public: |
|
robertphillips
2014/06/04 18:53:36
Don't think you need explicit here.
this->loadPKM(
krajcevski
2014/06/04 19:26:53
Done.
|
| + explicit ETCBitmapBenchBase() : fPKMData(loadPKM()) { |
| + if (NULL == fPKMData) { |
| + SkDebugf("Could not load PKM data!"); |
| + } |
| + } |
| + |
| +protected: |
| + SkAutoDataUnref fPKMData; |
| + |
| +private: |
| + SkData *loadPKM() { |
| + SkString filename = SkOSPath::SkPathJoin( |
| + INHERITED::GetResourcePath().c_str(), "mandrill_128.pkm"); |
| + |
| + // Expand the data |
| + SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str())); |
| + if (NULL == fileData) { |
| + SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
| + return NULL; |
| + } |
| + |
|
robertphillips
2014/06/04 18:53:36
static const etc1_uint32 kExpansionFactor = 8; ?
krajcevski
2014/06/04 19:26:53
Done.
|
| + const etc1_uint32 factor = 8; |
| + etc1_byte* expandedETC1 = create_expanded_etc1_bitmap(fileData->bytes(), factor); |
| + if (NULL == expandedETC1) { |
| + SkDebugf("Error expanding ETC1 data by factor of %d\n", factor); |
| + return NULL; |
| + } |
| + |
| + 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); |
| + } |
| + |
| + typedef SkBenchmark INHERITED; |
| +}; |
| + |
| +// This is the rendering benchmark. Prior to rendering the data, create a |
| +// bitmap using the etc1 data. We repurpose the isSuitableFor override since it |
| +// gets called on every config outside of the timing loop. |
| +class ETCBitmapBench : public ETCBitmapBenchBase { |
| +public: |
| + explicit ETCBitmapBench(bool decompress) : fDecompress(decompress) { } |
| + |
| +protected: |
| + virtual const char* onGetName() SK_OVERRIDE { |
| + if (fDecompress) { |
| + return "etc1bitmap_render_decompressed"; |
| + } else { |
| + return "etc1bitmap_render_compressed"; |
| + } |
| + } |
| + |
|
robertphillips
2014/06/04 18:53:36
I think we should actually parameterize the bench
krajcevski
2014/06/04 19:26:53
Done.
|
| + virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| + // !HACK! Since this function gets called on every config |
| + // we can create our bitmap here so that it doesn't get |
| + // decompressed in 8888 and then rendered as a bitmap in |
| + // the gpu... |
| + if (NULL == fPKMData) { |
| + return false; |
| + } |
| + |
| + // Install pixel ref |
| + if (!SkInstallDiscardablePixelRef( |
| + SkDecodingImageGenerator::Create( |
| + fPKMData, SkDecodingImageGenerator::Options()), &(this->fBitmap))) { |
| + SkDebugf("Could not install discardable pixel ref.\n"); |
| + // !HACK! If there was an error don't bother rendering for this config... |
| + return false; |
| + } |
| + |
| + // Decompress it if necessary |
| + if (this->fDecompress) { |
| + this->fBitmap.lockPixels(); |
| + } |
| + |
| + // What this function is actually for |
| + return backend == kRaster_Backend || backend == kGPU_Backend; |
| + } |
| + |
| + virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| + for (int i = 0; i < loops; i++) { |
| + canvas->drawBitmap(this->fBitmap, 0, 0, NULL); |
| + } |
| + } |
| + |
| +protected: |
| + SkBitmap fBitmap; |
| + bool decompress() const { return fDecompress; } |
| +private: |
| + const bool fDecompress; |
| + 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: |
| + explicit ETCBitmapUploadBench(bool decompress) : ETCBitmapBench(decompress) { } |
| + |
| +protected: |
| + virtual const char* onGetName() SK_OVERRIDE { |
| + if (this->decompress()) { |
| + return "etc1bitmap_upload_decompressed"; |
| + } else { |
| + return "etc1bitmap_upload_compressed"; |
| + } |
| + } |
| + |
| + virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| + for (int i = 0; i < loops; i++) { |
| + this->fBitmap.notifyPixelsChanged(); |
| + canvas->drawBitmap(this->fBitmap, 0, 0, NULL); |
| + } |
| + } |
| + |
| +private: |
| + typedef ETCBitmapBenchBase INHERITED; |
| +}; |
| + |
| +DEF_BENCH(return new ETCBitmapBench(false);) |
| +DEF_BENCH(return new ETCBitmapBench(true);) |
| + |
| +DEF_BENCH(return new ETCBitmapUploadBench(false);) |
| +DEF_BENCH(return new ETCBitmapUploadBench(true);) |
| + |
| +#endif // SK_IGNORE_ETC1_SUPPORT |