2012-08-01 19:34:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "Test.h"
|
2012-08-01 19:34:20 +00:00
|
|
|
#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"
|
|
|
|
|
2014-01-02 22:20:49 +00:00
|
|
|
struct SkFlattenableTraits {
|
|
|
|
static void flatten(SkOrderedWriteBuffer& buffer, const SkFlattenable& flattenable) {
|
|
|
|
buffer.writeFlattenable(&flattenable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct SkBitmapTraits {
|
|
|
|
static void flatten(SkOrderedWriteBuffer& buffer, const SkBitmap& bitmap) {
|
|
|
|
bitmap.flatten(buffer);
|
|
|
|
}
|
|
|
|
};
|
2012-08-01 19:34:20 +00:00
|
|
|
|
2012-08-02 18:49:49 +00:00
|
|
|
class Controller : public SkChunkFlatController {
|
|
|
|
public:
|
|
|
|
Controller() : INHERITED(1024) {
|
|
|
|
this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef SkChunkFlatController INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-08-01 19:34:20 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-01-02 22:20:49 +00:00
|
|
|
template <typename Traits, typename T>
|
|
|
|
static void testCreate(skiatest::Reporter* reporter, const T& obj) {
|
2012-08-02 18:49:49 +00:00
|
|
|
Controller controller;
|
2014-01-02 22:20:49 +00:00
|
|
|
// 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);
|
2013-08-20 16:48:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, *data1 == *data2);
|
2012-08-01 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(FlatData, reporter) {
|
2012-08-01 19:34:20 +00:00
|
|
|
// 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;
|
2014-01-02 22:20:49 +00:00
|
|
|
|
|
|
|
SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2,
|
|
|
|
SkShader::kRepeat_TileMode));
|
|
|
|
testCreate<SkFlattenableTraits>(reporter, *shader);
|
2012-08-01 19:34:20 +00:00
|
|
|
|
|
|
|
// 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);
|
2014-01-02 22:20:49 +00:00
|
|
|
testCreate<SkBitmapTraits>(reporter, bm);
|
2012-08-01 19:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test SkColorFilter
|
2014-01-02 22:20:49 +00:00
|
|
|
SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED));
|
|
|
|
testCreate<SkFlattenableTraits>(reporter, *cf);
|
2012-08-01 19:34:20 +00:00
|
|
|
|
|
|
|
// Test SkXfermode
|
2014-01-02 22:20:49 +00:00
|
|
|
SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
|
|
|
|
testCreate<SkFlattenableTraits>(reporter, *xfer);
|
2012-08-01 19:34:20 +00:00
|
|
|
}
|