skia2/tests/FlatDataTest.cpp
commit-bot@chromium.org 8b0e8ac5f5 Refactor read and write buffers.
Eliminates SkFlattenable{Read,Write}Buffer, promoting SkOrdered{Read,Write}Buffer
a step each in the hierarchy.

What used to be this:

SkFlattenableWriteBuffer -> SkOrderedWriteBuffer
SkFlattenableReadBuffer  -> SkOrderedReadBuffer
SkFlattenableReadBuffer  -> SkValidatingReadBuffer

is now

SkWriteBuffer
SkReadBuffer -> SkValidatingReadBuffer

Benefits:
  - code is simpler, names are less wordy
  - the generic SkFlattenableFooBuffer code in SkPaint was incorrect; removed
  - write buffers are completely devirtualized, important for record speed

This refactoring was mostly mechanical.  You aren't going to find anything
interesting in files with less than 10 lines changed.

BUG=skia:
R=reed@google.com, scroggo@google.com, djsollen@google.com, mtklein@google.com

Author: mtklein@chromium.org

Review URL: https://codereview.chromium.org/134163010

git-svn-id: http://skia.googlecode.com/svn/trunk@13245 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-30 18:58:24 +00:00

88 lines
2.7 KiB
C++

/*
* 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);
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);
}