122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org/*
222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org * Copyright 2013 Google Inc.
322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org *
422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org * found in the LICENSE file.
622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org */
722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SampleCode.h"
8067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkAlphaThresholdFilter.h"
922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkBitmapSource.h"
1022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkBlurImageFilter.h"
1122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkCanvas.h"
1222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkColorFilter.h"
1322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkColorFilterImageFilter.h"
1422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkComposeImageFilter.h"
1577e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#include "SkData.h"
1622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkDisplacementMapEffect.h"
1722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkDropShadowImageFilter.h"
1822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkFlattenableSerialization.h"
1922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkLightingImageFilter.h"
2022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMagnifierImageFilter.h"
21067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkMatrixImageFilter.h"
22067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkMatrixConvolutionImageFilter.h"
2322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMergeImageFilter.h"
2422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMorphologyImageFilter.h"
2522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkOffsetImageFilter.h"
2622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkPerlinNoiseShader.h"
277b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org#include "SkPictureImageFilter.h"
2897f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org#include "SkPictureRecorder.h"
2922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkRandom.h"
3022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkRectShaderImageFilter.h"
31067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkTestImageFilters.h"
32cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#include "SkTileImageFilter.h"
3322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkView.h"
3422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkXfermodeImageFilter.h"
35cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#include <stdio.h>
36cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#include <time.h>
3722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
38c2e9db30d393862bd3485cfe57b4ac06433f2f32commit-bot@chromium.org//#define SK_ADD_RANDOM_BIT_FLIPS
39c2e9db30d393862bd3485cfe57b4ac06433f2f32commit-bot@chromium.org//#define SK_FUZZER_IS_VERBOSE
40c2e9db30d393862bd3485cfe57b4ac06433f2f32commit-bot@chromium.org
41cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.orgstatic const uint32_t kSeed = (uint32_t)(time(NULL));
4222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkRandom gRand(kSeed);
4322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic bool return_large = false;
4422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic bool return_undef = false;
4522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
4622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic const int kBitmapSize = 24;
4722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
4822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic int R(float x) {
4922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
5022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
5122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
5222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#if defined _WIN32
5322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#pragma warning ( push )
5422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org// we are intentionally causing an overflow here
5522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//      (warning C4756: overflow in constant arithmetic)
5622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#pragma warning ( disable : 4756 )
5722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#endif
5822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
5922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic float huge() {
6022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    double d = 1e100;
6122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    float f = (float)d;
6222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return f;
6322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
6422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
6522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#if defined _WIN32
6622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#pragma warning ( pop )
6722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#endif
6822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
6922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic float make_number(bool positiveOnly) {
7022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    float f = positiveOnly ? 1.0f : 0.0f;
7122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    float v = f;
7222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    int sel;
7322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
7422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (return_large) sel = R(6); else sel = R(4);
7522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!return_undef && sel == 0) sel = 1;
7622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
7722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (R(2) == 1) v = (float)(R(100)+f); else
7822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
7922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    switch (sel) {
8022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 0: break;
8122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 1: v = f; break;
8222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 2: v = 0.000001f; break;
8322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 3: v = 10000.0f; break;
8422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 4: v = 2000000000.0f; break;
8522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 5: v = huge(); break;
8622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
8722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
8822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!positiveOnly && (R(4) == 1)) v = -v;
8922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return v;
9022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
9122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
9222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkScalar make_scalar(bool positiveOnly = false) {
934b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    return make_number(positiveOnly);
9422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
9522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
96cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.orgstatic SkRect make_rect() {
97cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))),
98cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                          SkIntToScalar(R(static_cast<float>(kBitmapSize))));
9922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
10022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
101067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.orgstatic SkRegion make_region() {
102b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org    SkIRect iRegion = SkIRect::MakeXYWH(R(static_cast<float>(kBitmapSize)),
103b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                        R(static_cast<float>(kBitmapSize)),
104b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                        R(static_cast<float>(kBitmapSize)),
105b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                        R(static_cast<float>(kBitmapSize)));
106067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    return SkRegion(iRegion);
107067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org}
108067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org
109067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.orgstatic SkMatrix make_matrix() {
110067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    SkMatrix m;
111067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    for (int i = 0; i < 9; ++i) {
112067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        m[i] = make_scalar();
113067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    }
114067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    return m;
115067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org}
116067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org
11722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkXfermode::Mode make_xfermode() {
11822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1));
11922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
12022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
12122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkColor make_color() {
12222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090;
12322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
12422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
12522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkPoint3 make_point() {
12622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return SkPoint3(make_scalar(), make_scalar(), make_scalar(true));
12722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
12822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
12922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() {
13022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1);
13122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
13222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
133a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic bool valid_for_raster_canvas(const SkImageInfo& info) {
134a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    switch (info.colorType()) {
135a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        case kAlpha_8_SkColorType:
136a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        case kRGB_565_SkColorType:
137a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            return true;
13828fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        case kN32_SkColorType:
139a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            return kPremul_SkAlphaType == info.alphaType() ||
140a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                   kOpaque_SkAlphaType == info.alphaType();
141a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        default:
142a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            break;
143ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    }
144a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    return false;
145a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
146a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
147a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic SkColorType rand_colortype() {
148a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    return (SkColorType)R(kLastEnum_SkColorType + 1);
149a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
150a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
151a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic void rand_bitmap_for_canvas(SkBitmap* bitmap) {
152a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkImageInfo info;
153a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    do {
154a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
155a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                                 kPremul_SkAlphaType);
156848250415eddc54075f7eb8795e8db79e749c6abreed    } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info));
157a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
158a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
159a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic void make_g_bitmap(SkBitmap& bitmap) {
160a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    rand_bitmap_for_canvas(&bitmap);
161a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
162a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkCanvas canvas(bitmap);
16322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.clear(0x00000000);
16422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint paint;
16522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setAntiAlias(true);
16622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setColor(0xFF884422);
16722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setTextSize(SkIntToScalar(kBitmapSize/2));
16822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const char* str = "g";
16922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
17022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                    SkIntToScalar(kBitmapSize/4), paint);
17122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
17222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
17322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic void make_checkerboard_bitmap(SkBitmap& bitmap) {
174a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    rand_bitmap_for_canvas(&bitmap);
175a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
176a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkCanvas canvas(bitmap);
17722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.clear(0x00000000);
17822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint darkPaint;
17922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    darkPaint.setColor(0xFF804020);
18022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint lightPaint;
18122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    lightPaint.setColor(0xFF244484);
18222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const int i = kBitmapSize / 8;
18322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const SkScalar f = SkIntToScalar(i);
18422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    for (int y = 0; y < kBitmapSize; y += i) {
18522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        for (int x = 0; x < kBitmapSize; x += i) {
18622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.save();
18722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
18822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
18922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
19022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
19122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
19222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.restore();
19322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        }
19422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
19522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
19622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
19722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic const SkBitmap& make_bitmap() {
19822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    static SkBitmap bitmap[2];
19922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    static bool initialized = false;
20022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!initialized) {
20122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        make_g_bitmap(bitmap[0]);
20222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        make_checkerboard_bitmap(bitmap[1]);
20322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        initialized = true;
20422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
20522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return bitmap[R(2)];
20622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
20722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
2085e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.orgstatic void drawSomething(SkCanvas* canvas) {
2095e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    SkPaint paint;
2105e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2115e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->save();
2125e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->scale(0.5f, 0.5f);
2135e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawBitmap(make_bitmap(), 0, 0, NULL);
2145e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->restore();
2155e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2165e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    const char beforeStr[] = "before circle";
2175e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    const char afterStr[] = "after circle";
2185e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2195e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setAntiAlias(true);
2205e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2215e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setColor(SK_ColorRED);
2225e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawData(beforeStr, sizeof(beforeStr));
2235e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
2245e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawData(afterStr, sizeof(afterStr));
2255e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setColor(SK_ColorBLACK);
2265e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setTextSize(SkIntToScalar(kBitmapSize/3));
2275e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
2285e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org}
2295e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
23022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkImageFilter* make_image_filter(bool canBeNull = true) {
23122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkImageFilter* filter = 0;
23222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
23322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    // Add a 1 in 3 chance to get a NULL input
23422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (canBeNull && (R(3) == 1)) { return filter; }
23522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
2367bf106847304840526a3e722b5ff5ce7322a2826senorblanco    enum { ALPHA_THRESHOLD, MERGE, COLOR, BLUR, MAGNIFIER,
237067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org           DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
23822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org           DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
2397b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org           MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, NUM_FILTERS };
24022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
24122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    switch (R(NUM_FILTERS)) {
242067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case ALPHA_THRESHOLD:
243067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkAlphaThresholdFilter::Create(make_region(), make_scalar(), make_scalar());
244067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
24522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MERGE:
246cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkMergeImageFilter::Create(make_image_filter(), make_image_filter(), make_xfermode());
24722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
24822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case COLOR:
24922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
25022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ?
25122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) :
25222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkColorFilter::CreateLightingFilter(make_color(), make_color()));
25322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0;
25422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
25522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
25622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BLUR:
257cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkBlurImageFilter::Create(make_scalar(true), make_scalar(true), make_image_filter());
25822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
25922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MAGNIFIER:
260cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkMagnifierImageFilter::Create(make_rect(), make_scalar(true));
26122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
262067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case DOWN_SAMPLE:
263067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkDownSampleImageFilter::Create(make_scalar());
264067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
26522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case XFERMODE:
26622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
26722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode()));
268cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkXfermodeImageFilter::Create(mode, make_image_filter(), make_image_filter());
26922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
27022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
27122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case OFFSET:
272cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkOffsetImageFilter::Create(make_scalar(), make_scalar(), make_image_filter());
27322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
274067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case MATRIX:
275067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkMatrixImageFilter::Create(make_matrix(),
276067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                             (SkPaint::FilterLevel)R(4),
277067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                             make_image_filter());
278067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
279067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case MATRIX_CONVOLUTION:
280067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    {
281067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
282067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                     SkIntToScalar(kBitmapSize)));
283067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkISize size = SkISize::Make(R(10)+1, R(10)+1);
284067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        int arraySize = size.width() * size.height();
285067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkTArray<SkScalar> kernel(arraySize);
286067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        for (int i = 0; i < arraySize; ++i) {
287067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org            kernel.push_back() = make_scalar();
288067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        }
289b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org        SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
290b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                               R(SkIntToScalar(size.height())));
291067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkMatrixConvolutionImageFilter::Create(size,
292067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        kernel.begin(),
293067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_scalar(),
294067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_scalar(),
295067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        kernelOffset,
296067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        (SkMatrixConvolutionImageFilter::TileMode)R(3),
297067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        R(2) == 1,
298067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_image_filter(),
299067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        &cropR);
300067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    }
301067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
30222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case COMPOSE:
303cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkComposeImageFilter::Create(make_image_filter(), make_image_filter());
30422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
30522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DISTANT_LIGHT:
30622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
30722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateDistantLitDiffuse(make_point(),
30822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
30922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateDistantLitSpecular(make_point(),
31022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
31122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_image_filter());
31222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
31322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case POINT_LIGHT:
31422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
31522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreatePointLitDiffuse(make_point(),
31622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
31722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreatePointLitSpecular(make_point(),
31822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
31922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_image_filter());
32022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
32122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case SPOT_LIGHT:
32222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
32322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3(0, 0, 0),
32422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_point(), make_scalar(), make_scalar(), make_color(),
32522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_scalar(), make_scalar(), make_image_filter()) :
32622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3(0, 0, 0),
32722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_point(), make_scalar(), make_scalar(), make_color(),
32822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_scalar(), make_scalar(), SkIntToScalar(R(10)), make_image_filter());
32922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
33022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case NOISE:
33122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
33222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkShader> shader((R(2) == 1) ?
33322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            SkPerlinNoiseShader::CreateFractalNoise(
334bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) :
3359fbbcca1c958e6df2cff24d3ccdb7ebd89b8486bcommit-bot@chromium.org            SkPerlinNoiseShader::CreateTurbulence(
336bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()));
3374469938e92d779dff05e745559e67907bbf21e78reed@google.com        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
3384469938e92d779dff05e745559e67907bbf21e78reed@google.com                                                     SkIntToScalar(kBitmapSize)));
3394469938e92d779dff05e745559e67907bbf21e78reed@google.com        filter = SkRectShaderImageFilter::Create(shader, &cropR);
34022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
34122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
34222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DROP_SHADOW:
343cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkDropShadowImageFilter::Create(make_scalar(), make_scalar(),
344bc9845bb60cd883e7ddabb21f04997602145a1adsenorblanco                     make_scalar(true), make_scalar(true), make_color(), make_image_filter());
34522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
34622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MORPHOLOGY:
34743f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        if (R(2) == 1) {
348cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkDilateImageFilter::Create(R(static_cast<float>(kBitmapSize)),
349bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                R(static_cast<float>(kBitmapSize)), make_image_filter());
35043f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        } else {
351cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkErodeImageFilter::Create(R(static_cast<float>(kBitmapSize)),
352bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                R(static_cast<float>(kBitmapSize)), make_image_filter());
35343f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        }
35422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
35522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BITMAP:
35643f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        if (R(2) == 1) {
357cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkBitmapSource::Create(make_bitmap(), make_rect(), make_rect());
35843f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        } else {
359cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkBitmapSource::Create(make_bitmap());
36043f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        }
36122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
36222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DISPLACE:
363cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkDisplacementMapEffect::Create(make_channel_selector_type(),
364cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                 make_channel_selector_type(), make_scalar(),
365cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                 make_image_filter(false), make_image_filter());
36622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
367cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    case TILE:
368cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkTileImageFilter::Create(make_rect(), make_rect(), make_image_filter(false));
369cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        break;
3707b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org    case PICTURE:
3715e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    {
37297f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkRTreeFactory factory;
37397f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkPictureRecorder recorder;
374a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips        SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
375a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                                                            SkIntToScalar(kBitmapSize),
376a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                                                            &factory, 0);
37797f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        drawSomething(recordingCanvas);
37897f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkAutoTUnref<SkPicture> pict(recorder.endRecording());
37997f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        filter = SkPictureImageFilter::Create(pict.get(), make_rect());
3805e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    }
3817b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org        break;
38222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    default:
38322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
38422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
38522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
38622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
38722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
38877e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.orgstatic SkImageFilter* make_serialized_image_filter() {
38977e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkAutoTUnref<SkImageFilter> filter(make_image_filter(false));
39077e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter));
39177e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
39277e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    size_t len = data->size();
39377e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#ifdef SK_ADD_RANDOM_BIT_FLIPS
39477e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    unsigned char* p = const_cast<unsigned char*>(ptr);
39577e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    for (size_t i = 0; i < len; ++i, ++p) {
396cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
397cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            if (R(10) == 1) { // Then 10% of the time, change a whole byte
398cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                switch(R(3)) {
399cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 0:
400cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p ^= 0xFF; // Flip entire byte
401cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
402cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 1:
403cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p = 0xFF; // Set all bits to 1
404cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
405cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 2:
406cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p = 0x00; // Set all bits to 0
407cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
408cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                }
409cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            } else {
410cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                *p ^= (1 << R(8));
411cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            }
41277e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org        }
41377e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    }
41477e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#endif // SK_ADD_RANDOM_BIT_FLIPS
41577e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
41677e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org                                    SkImageFilter::GetFlattenableType());
41777e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    return static_cast<SkImageFilter*>(flattenable);
41877e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org}
41977e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org
420bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.comstatic void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
42122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->save();
42222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
42322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
42422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
42522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->restore();
42622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
42722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
42822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic void do_fuzz(SkCanvas* canvas) {
429ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    SkImageFilter* filter = make_serialized_image_filter();
430ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org
431cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#ifdef SK_FUZZER_IS_VERBOSE
432ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    static uint32_t numFilters = 0;
433ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    static uint32_t numValidFilters = 0;
434ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    if (0 == numFilters) {
435cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        printf("Fuzzing with %u\n", kSeed);
436cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    }
437ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    numFilters++;
43849f085dddff10473b6ebf832a974288300224e60bsalomon    if (filter) {
439ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        numValidFilters++;
440ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    }
441ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
442cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    fflush(stdout);
443cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#endif
444cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org
44522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint paint;
446ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    SkSafeUnref(paint.setImageFilter(filter));
44722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    drawClippedBitmap(canvas, 0, 0, paint);
44822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
44922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
45022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
45122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
45222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgclass ImageFilterFuzzView : public SampleView {
45322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgpublic:
45422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    ImageFilterFuzzView() {
45522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        this->setBGColor(0xFFDDDDDD);
45622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
45722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
45822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgprotected:
45922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    // overrides from SkEventSink
46022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    virtual bool onQuery(SkEvent* evt) {
46122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        if (SampleCode::TitleQ(*evt)) {
46222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            SampleCode::TitleR(evt, "ImageFilterFuzzer");
46322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            return true;
46422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        }
46522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        return this->INHERITED::onQuery(evt);
46622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
46722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
46822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    void drawBG(SkCanvas* canvas) {
46922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        canvas->drawColor(0xFFDDDDDD);
47022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
47122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
47222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    virtual void onDrawContent(SkCanvas* canvas) {
47322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        do_fuzz(canvas);
47422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        this->inval(0);
47522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
47622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
47722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgprivate:
47822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    typedef SkView INHERITED;
47922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org};
48022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
48122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
48222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
48322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkView* MyFactory() { return new ImageFilterFuzzView; }
48422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkViewRegister reg(MyFactory);
485