Enabling validation code in serialization and adding serialization to fuzzer

BUG=
R=reed@google.com, mtklein@google.com, senorblanco@chromium.org, bsalomon@google.com

Author: sugoi@chromium.org

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

git-svn-id: http://skia.googlecode.com/svn/trunk@11968 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2013-10-25 21:04:40 +00:00
parent 10ba006631
commit d25d6c7b8e
3 changed files with 40 additions and 16 deletions

View File

@ -8,12 +8,19 @@
#ifndef SkFlattenableSerialization_DEFINED
#define SkFlattenableSerialization_DEFINED
#include "SkTypes.h"
#include "SkFlattenable.h"
class SkData;
class SkFlattenable;
SK_API SkData* SkSerializeFlattenable(SkFlattenable*);
SK_API SkFlattenable* SkDeserializeFlattenable(const void* data, size_t size);
SK_API SkData* SkValidatingSerializeFlattenable(SkFlattenable*);
SK_API SkFlattenable* SkValidatingDeserializeFlattenable(const void* data, size_t size,
SkFlattenable::Type type);
// Temporary fix for canary build
#define SkSerializeFlattenable(flattenable) \
SkValidatingSerializeFlattenable(flattenable)
#define SkDeserializeFlattenable(data, size) \
SkValidatingDeserializeFlattenable(data, size, SkFlattenable::kSkImageFilter_Type)
#endif

View File

@ -1,4 +1,3 @@
/*
* Copyright 2013 Google Inc.
*
@ -7,13 +6,14 @@
*/
#include "SampleCode.h"
#include "SkBicubicImageFilter.h"
#include "SkBitmapDevice.h"
#include "SkBitmapSource.h"
#include "SkBlurImageFilter.h"
#include "SkCanvas.h"
#include "SkColorFilter.h"
#include "SkColorFilterImageFilter.h"
#include "SkComposeImageFilter.h"
#include "SkBitmapDevice.h"
#include "SkData.h"
#include "SkDisplacementMapEffect.h"
#include "SkDropShadowImageFilter.h"
#include "SkFlattenableSerialization.h"
@ -262,6 +262,25 @@ static SkImageFilter* make_image_filter(bool canBeNull = true) {
return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
}
static SkImageFilter* make_serialized_image_filter() {
SkAutoTUnref<SkImageFilter> filter(make_image_filter(false));
SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter));
const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
size_t len = data->size();
#ifdef SK_ADD_RANDOM_BIT_FLIPS
unsigned char* p = const_cast<unsigned char*>(ptr);
for (size_t i = 0; i < len; ++i, ++p) {
if ((R(1000) == 1)) { // 0.1% of the time, flip a bit
*p ^= (1 << R(8));
}
}
#endif // SK_ADD_RANDOM_BIT_FLIPS
SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
SkImageFilter::GetFlattenableType());
SkASSERT(NULL != flattenable);
return static_cast<SkImageFilter*>(flattenable);
}
static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
canvas->save();
canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
@ -272,7 +291,7 @@ static void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& pai
static void do_fuzz(SkCanvas* canvas) {
SkPaint paint;
paint.setImageFilter(make_image_filter());
paint.setImageFilter(make_serialized_image_filter())->unref();
drawClippedBitmap(canvas, 0, 0, paint);
}

View File

@ -8,14 +8,12 @@
#include "SkFlattenableSerialization.h"
#include "SkData.h"
#include "SkFlattenable.h"
#include "SkImageFilter.h"
#include "SkOrderedReadBuffer.h"
#include "SkValidatingReadBuffer.h"
#include "SkOrderedWriteBuffer.h"
SkData* SkSerializeFlattenable(SkFlattenable* flattenable) {
SkData* SkValidatingSerializeFlattenable(SkFlattenable* flattenable) {
SkOrderedWriteBuffer writer(1024);
writer.setFlags(SkOrderedWriteBuffer::kCrossProcess_Flag);
writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
writer.writeFlattenable(flattenable);
uint32_t size = writer.bytesWritten();
void* data = sk_malloc_throw(size);
@ -23,8 +21,8 @@ SkData* SkSerializeFlattenable(SkFlattenable* flattenable) {
return SkData::NewFromMalloc(data, size);
}
// TODO: this guy should be renamed to ImageFilter, or take SkFlattenable::Type as a parameter.
SkFlattenable* SkDeserializeFlattenable(const void* data, size_t size) {
SkOrderedReadBuffer buffer(data, size);
return buffer.readImageFilter();
SkFlattenable* SkValidatingDeserializeFlattenable(const void* data, size_t size,
SkFlattenable::Type type) {
SkValidatingReadBuffer buffer(data, size);
return buffer.readFlattenable(type);
}