skia2/tests/FlatDataTest.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkColor.h"
#include "SkColorFilter.h"
#include "SkGradientShader.h"
#include "SkPaint.h"
#include "SkPictureFlat.h"
#include "SkShader.h"
#include "SkXfermode.h"
#include "Test.h"
struct SkFlattenableTraits {
static void flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
buffer.writeFlattenable(&flattenable);
}
};
struct SkBitmapTraits {
static void flatten(SkWriteBuffer& buffer, const SkBitmap& bitmap) {
bitmap.flatten(buffer);
}
};
class Controller : public SkChunkFlatController {
public:
Controller() : INHERITED(1024) {
this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref();
}
private:
typedef SkChunkFlatController INHERITED;
};
/**
* Verify that two SkFlatData objects that created from the same object are
* identical when using an SkNamedFactorySet.
* @param reporter Object to report failures.
* @param obj Flattenable object to be flattened.
* @param flattenProc Function that flattens objects with the same type as obj.
*/
template <typename Traits, typename T>
static void testCreate(skiatest::Reporter* reporter, const T& obj) {
Controller controller;
// No need to delete data because that will be taken care of by the controller.
SkFlatData* data1 = SkFlatData::Create<Traits>(&controller, obj, 0);
SkFlatData* data2 = SkFlatData::Create<Traits>(&controller, obj, 1);
use SkTDynamicHash in picture recording cleaned up SkPictureFlat.h quite a bit while working on this. bench --match picture_record_ shows some improvement: compare.sh bench --match picture_record_ --repeat 100 master -> usehash N=3 p=0.001000 (corrected to 0.000333) sig? rel. speed bench y 1.0x picture_record_dictionaries y 1.5x picture_record_recurring_paint_dictionary y 3.8x picture_record_unique_paint_dictionary Overall relative speed: 1.9x bench_pictures --record is pretty much neutral: compare.sh bench_pictures -r ../skp --mode record --repeat 30 master -> usehash N=63 p=0.001000 (corrected to 0.000016) sig? rel. speed bench n 0.9x desk_pokemonwiki.skp y 0.9x desk_googlespreadsheet.skp y 0.9x tabl_pravda.skp y 1.0x desk_googlespreadsheetdashed.skp n 1.0x tabl_onlinewsj.skp n 1.0x tabl_nytimes.skp n 1.0x desk_googlehome.skp y 1.0x desk_techcrunch.skp n 1.0x tabl_slashdot.skp n 1.0x tabl_techmeme.skp n 1.0x desk_googleplus.skp n 1.0x desk_sfgate.skp n 1.0x tabl_transformice.skp n 1.0x desk_espn.skp n 1.0x desk_baidu.skp n 1.0x tabl_worldjournal.skp n 1.0x desk_chalkboard.skp n 1.0x tabl_frantzen.skp n 1.0x desk_gws.skp n 1.0x tabl_androidpolice.skp n 1.0x desk_linkedin.skp n 1.0x mobi_wikipedia.skp n 1.0x desk_wowwiki.skp n 1.0x desk_css3gradients.skp n 1.0x desk_gmailthread.skp n 1.0x desk_yahoogames.skp n 1.0x desk_facebook.skp n 1.0x desk_wordpress.skp n 1.0x tabl_vnexpress.skp n 1.0x desk_br337.skp n 1.0x tabl_engadget.skp n 1.0x tabl_theverge.skp n 1.0x desk_amazon.skp n 1.0x desk_ebay.skp n 1.0x tabl_hsfi.skp n 1.0x tabl_sahadan.skp n 1.0x desk_weather.skp n 1.0x tabl_digg.skp n 1.0x desk_youtubetvbrowse.skp n 1.0x tabl_culturalsolutions.skp n 1.0x tabl_ukwsj.skp n 1.0x desk_youtube.skp n 1.0x tabl_googlecalendar.skp y 1.0x desk_yahooanswers.skp n 1.0x desk_blogger.skp n 1.0x desk_yahoonews.skp y 1.0x desk_yahoosports.skp y 1.0x tabl_mercurynews.skp n 1.0x desk_youtubetvvideo.skp y 1.0x tabl_gspro.skp y 1.1x tabl_googleblog.skp y 1.1x tabl_cnet.skp y 1.1x tabl_mlb.skp y 1.1x tabl_cuteoverload.skp y 1.1x desk_booking.skp y 1.1x tabl_deviantart.skp y 1.1x desk_twitter.skp y 1.1x tabl_cnn.skp y 1.1x tabl_gamedeksiam.skp y 1.1x tabl_gmail.skp y 1.1x tabl_nofolo.skp y 1.1x tabl_mozilla.skp y 1.1x desk_pinterest.skp Overall relative speed: 1.0x (I'd take this to mean that the microbenches are probably drifting away from relevance.) BUG= R=reed@google.com Review URL: https://codereview.chromium.org/21564008 git-svn-id: http://skia.googlecode.com/svn/trunk@10825 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-08-20 16:48:47 +00:00
REPORTER_ASSERT(reporter, *data1 == *data2);
}
DEF_TEST(FlatData, reporter) {
// Test flattening SkShader
SkPoint points[2];
points[0].set(0, 0);
points[1].set(SkIntToScalar(20), SkIntToScalar(20));
SkColor colors[2];
colors[0] = SK_ColorRED;
colors[1] = SK_ColorBLUE;
SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2,
SkShader::kRepeat_TileMode));
testCreate<SkFlattenableTraits>(reporter, *shader);
// Test SkBitmap
{
SkBitmap bm;
bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50);
bm.allocPixels();
SkCanvas canvas(bm);
SkPaint paint;
paint.setShader(shader);
canvas.drawPaint(paint);
testCreate<SkBitmapTraits>(reporter, bm);
}
// Test SkColorFilter
SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED));
testCreate<SkFlattenableTraits>(reporter, *cf);
// Test SkXfermode
SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
testCreate<SkFlattenableTraits>(reporter, *xfer);
}