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"
12f11c57488205faddd10dffedcd3c122aac1935adsugoi#include "SkColorCubeFilter.h"
1322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkColorFilter.h"
1422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkColorFilterImageFilter.h"
1522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkComposeImageFilter.h"
1677e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#include "SkData.h"
1722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkDisplacementMapEffect.h"
1822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkDropShadowImageFilter.h"
1922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkFlattenableSerialization.h"
2022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkLightingImageFilter.h"
2122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMagnifierImageFilter.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
125234f036b3e731e06e616c5291157d3bb4fbfdee2sugoistatic SkDropShadowImageFilter::ShadowMode make_shadow_mode() {
126234f036b3e731e06e616c5291157d3bb4fbfdee2sugoi    return (R(2) == 1) ? SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode :
127234f036b3e731e06e616c5291157d3bb4fbfdee2sugoi                         SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode;
128234f036b3e731e06e616c5291157d3bb4fbfdee2sugoi}
129234f036b3e731e06e616c5291157d3bb4fbfdee2sugoi
13022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkPoint3 make_point() {
13122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return SkPoint3(make_scalar(), make_scalar(), make_scalar(true));
13222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
13322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
13422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() {
13522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1);
13622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
13722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
138a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic bool valid_for_raster_canvas(const SkImageInfo& info) {
139a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    switch (info.colorType()) {
140a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        case kAlpha_8_SkColorType:
141a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        case kRGB_565_SkColorType:
142a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            return true;
14328fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        case kN32_SkColorType:
144a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            return kPremul_SkAlphaType == info.alphaType() ||
145a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                   kOpaque_SkAlphaType == info.alphaType();
146a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        default:
147a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            break;
148ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    }
149a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    return false;
150a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
151a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
152a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic SkColorType rand_colortype() {
153a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    return (SkColorType)R(kLastEnum_SkColorType + 1);
154a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
155a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
156a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic void rand_bitmap_for_canvas(SkBitmap* bitmap) {
157a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkImageInfo info;
158a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    do {
159a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
160a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                                 kPremul_SkAlphaType);
161848250415eddc54075f7eb8795e8db79e749c6abreed    } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info));
162a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
163a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
164a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic void make_g_bitmap(SkBitmap& bitmap) {
165a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    rand_bitmap_for_canvas(&bitmap);
166a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
167a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkCanvas canvas(bitmap);
16822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.clear(0x00000000);
16922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint paint;
17022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setAntiAlias(true);
17122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setColor(0xFF884422);
17222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setTextSize(SkIntToScalar(kBitmapSize/2));
17322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const char* str = "g";
17422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
17522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                    SkIntToScalar(kBitmapSize/4), paint);
17622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
17722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
17822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic void make_checkerboard_bitmap(SkBitmap& bitmap) {
179a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    rand_bitmap_for_canvas(&bitmap);
180a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
181a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkCanvas canvas(bitmap);
18222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.clear(0x00000000);
18322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint darkPaint;
18422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    darkPaint.setColor(0xFF804020);
18522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint lightPaint;
18622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    lightPaint.setColor(0xFF244484);
18722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const int i = kBitmapSize / 8;
18822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const SkScalar f = SkIntToScalar(i);
18922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    for (int y = 0; y < kBitmapSize; y += i) {
19022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        for (int x = 0; x < kBitmapSize; x += i) {
19122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.save();
19222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
19322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
19422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
19522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
19622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
19722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.restore();
19822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        }
19922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
20022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
20122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
20222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic const SkBitmap& make_bitmap() {
20322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    static SkBitmap bitmap[2];
20422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    static bool initialized = false;
20522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!initialized) {
20622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        make_g_bitmap(bitmap[0]);
20722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        make_checkerboard_bitmap(bitmap[1]);
20822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        initialized = true;
20922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
21022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return bitmap[R(2)];
21122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
21222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
213f11c57488205faddd10dffedcd3c122aac1935adsugoistatic SkData* make_3Dlut(int* cubeDimension, bool invR, bool invG, bool invB) {
214f11c57488205faddd10dffedcd3c122aac1935adsugoi    int size = 4 << R(5);
215f11c57488205faddd10dffedcd3c122aac1935adsugoi    SkData* data = SkData::NewUninitialized(sizeof(SkColor) * size * size * size);
216f11c57488205faddd10dffedcd3c122aac1935adsugoi    SkColor* pixels = (SkColor*)(data->writable_data());
217f11c57488205faddd10dffedcd3c122aac1935adsugoi    SkAutoMalloc lutMemory(size);
218f11c57488205faddd10dffedcd3c122aac1935adsugoi    SkAutoMalloc invLutMemory(size);
219f11c57488205faddd10dffedcd3c122aac1935adsugoi    uint8_t* lut = (uint8_t*)lutMemory.get();
220f11c57488205faddd10dffedcd3c122aac1935adsugoi    uint8_t* invLut = (uint8_t*)invLutMemory.get();
221f11c57488205faddd10dffedcd3c122aac1935adsugoi    const int maxIndex = size - 1;
222f11c57488205faddd10dffedcd3c122aac1935adsugoi    for (int i = 0; i < size; i++) {
223f11c57488205faddd10dffedcd3c122aac1935adsugoi        lut[i] = (i * 255) / maxIndex;
224f11c57488205faddd10dffedcd3c122aac1935adsugoi        invLut[i] = ((maxIndex - i) * 255) / maxIndex;
225f11c57488205faddd10dffedcd3c122aac1935adsugoi    }
226f11c57488205faddd10dffedcd3c122aac1935adsugoi    for (int r = 0; r < size; ++r) {
227f11c57488205faddd10dffedcd3c122aac1935adsugoi        for (int g = 0; g < size; ++g) {
228f11c57488205faddd10dffedcd3c122aac1935adsugoi            for (int b = 0; b < size; ++b) {
229f11c57488205faddd10dffedcd3c122aac1935adsugoi                pixels[(size * ((size * b) + g)) + r] = SkColorSetARGB(0xFF,
230f11c57488205faddd10dffedcd3c122aac1935adsugoi                        invR ? invLut[r] : lut[r],
231f11c57488205faddd10dffedcd3c122aac1935adsugoi                        invG ? invLut[g] : lut[g],
232f11c57488205faddd10dffedcd3c122aac1935adsugoi                        invB ? invLut[b] : lut[b]);
233f11c57488205faddd10dffedcd3c122aac1935adsugoi            }
234f11c57488205faddd10dffedcd3c122aac1935adsugoi        }
235f11c57488205faddd10dffedcd3c122aac1935adsugoi    }
236f11c57488205faddd10dffedcd3c122aac1935adsugoi    if (cubeDimension) {
237f11c57488205faddd10dffedcd3c122aac1935adsugoi        *cubeDimension = size;
238f11c57488205faddd10dffedcd3c122aac1935adsugoi    }
239f11c57488205faddd10dffedcd3c122aac1935adsugoi    return data;
240f11c57488205faddd10dffedcd3c122aac1935adsugoi}
241f11c57488205faddd10dffedcd3c122aac1935adsugoi
2425e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.orgstatic void drawSomething(SkCanvas* canvas) {
2435e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    SkPaint paint;
2445e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2455e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->save();
2465e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->scale(0.5f, 0.5f);
2475e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawBitmap(make_bitmap(), 0, 0, NULL);
2485e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->restore();
2495e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2505e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setAntiAlias(true);
2515e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2525e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setColor(SK_ColorRED);
2535e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
2545e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setColor(SK_ColorBLACK);
2555e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setTextSize(SkIntToScalar(kBitmapSize/3));
2565e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
2575e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org}
2585e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
25922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkImageFilter* make_image_filter(bool canBeNull = true) {
26022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkImageFilter* filter = 0;
26122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
26222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    // Add a 1 in 3 chance to get a NULL input
26322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (canBeNull && (R(3) == 1)) { return filter; }
26422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
265f11c57488205faddd10dffedcd3c122aac1935adsugoi    enum { ALPHA_THRESHOLD, MERGE, COLOR, LUT3D, BLUR, MAGNIFIER,
266067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org           DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
26722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org           DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
2687b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org           MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, NUM_FILTERS };
26922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
27022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    switch (R(NUM_FILTERS)) {
271067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case ALPHA_THRESHOLD:
272067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkAlphaThresholdFilter::Create(make_region(), make_scalar(), make_scalar());
273067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
27422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MERGE:
275cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkMergeImageFilter::Create(make_image_filter(), make_image_filter(), make_xfermode());
27622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
27722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case COLOR:
27822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
27922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ?
28022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) :
28122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkColorFilter::CreateLightingFilter(make_color(), make_color()));
28222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0;
28322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
28422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
285f11c57488205faddd10dffedcd3c122aac1935adsugoi    case LUT3D:
286f11c57488205faddd10dffedcd3c122aac1935adsugoi    {
287f11c57488205faddd10dffedcd3c122aac1935adsugoi        int cubeDimension;
288f11c57488205faddd10dffedcd3c122aac1935adsugoi        SkAutoDataUnref lut3D(make_3Dlut(&cubeDimension, (R(2) == 1), (R(2) == 1), (R(2) == 1)));
289f11c57488205faddd10dffedcd3c122aac1935adsugoi        SkAutoTUnref<SkColorFilter> cf(SkColorCubeFilter::Create(lut3D, cubeDimension));
290f11c57488205faddd10dffedcd3c122aac1935adsugoi        filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0;
291f11c57488205faddd10dffedcd3c122aac1935adsugoi    }
292f11c57488205faddd10dffedcd3c122aac1935adsugoi        break;
29322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BLUR:
294cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkBlurImageFilter::Create(make_scalar(true), make_scalar(true), make_image_filter());
29522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
29622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MAGNIFIER:
297cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkMagnifierImageFilter::Create(make_rect(), make_scalar(true));
29822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
299067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case DOWN_SAMPLE:
300067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkDownSampleImageFilter::Create(make_scalar());
301067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
30222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case XFERMODE:
30322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
30422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode()));
305cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkXfermodeImageFilter::Create(mode, make_image_filter(), make_image_filter());
30622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
30722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
30822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case OFFSET:
309cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkOffsetImageFilter::Create(make_scalar(), make_scalar(), make_image_filter());
31022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
311067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case MATRIX:
3128c874eee943bdea0fab5b4d2707083c863e37c55senorblanco        filter = SkImageFilter::CreateMatrixFilter(make_matrix(),
3138c874eee943bdea0fab5b4d2707083c863e37c55senorblanco                                                   (SkFilterQuality)R(4),
3148c874eee943bdea0fab5b4d2707083c863e37c55senorblanco                                                   make_image_filter());
315067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
316067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case MATRIX_CONVOLUTION:
317067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    {
318067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
319067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                     SkIntToScalar(kBitmapSize)));
320067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkISize size = SkISize::Make(R(10)+1, R(10)+1);
321067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        int arraySize = size.width() * size.height();
322067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkTArray<SkScalar> kernel(arraySize);
323067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        for (int i = 0; i < arraySize; ++i) {
324067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org            kernel.push_back() = make_scalar();
325067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        }
326b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org        SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
327b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                               R(SkIntToScalar(size.height())));
328067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkMatrixConvolutionImageFilter::Create(size,
329067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        kernel.begin(),
330067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_scalar(),
331067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_scalar(),
332067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        kernelOffset,
333067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        (SkMatrixConvolutionImageFilter::TileMode)R(3),
334067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        R(2) == 1,
335067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_image_filter(),
336067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        &cropR);
337067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    }
338067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
33922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case COMPOSE:
340cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkComposeImageFilter::Create(make_image_filter(), make_image_filter());
34122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
34222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DISTANT_LIGHT:
34322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
34422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateDistantLitDiffuse(make_point(),
34522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
34622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateDistantLitSpecular(make_point(),
34722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
34822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_image_filter());
34922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
35022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case POINT_LIGHT:
35122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
35222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreatePointLitDiffuse(make_point(),
35322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
35422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreatePointLitSpecular(make_point(),
35522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
35622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_image_filter());
35722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
35822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case SPOT_LIGHT:
35922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
36022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3(0, 0, 0),
36122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_point(), make_scalar(), make_scalar(), make_color(),
36222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_scalar(), make_scalar(), make_image_filter()) :
36322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3(0, 0, 0),
36422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_point(), make_scalar(), make_scalar(), make_color(),
36522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_scalar(), make_scalar(), SkIntToScalar(R(10)), make_image_filter());
36622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
36722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case NOISE:
36822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
36922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkShader> shader((R(2) == 1) ?
37022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            SkPerlinNoiseShader::CreateFractalNoise(
371bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) :
3729fbbcca1c958e6df2cff24d3ccdb7ebd89b8486bcommit-bot@chromium.org            SkPerlinNoiseShader::CreateTurbulence(
373bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()));
3744469938e92d779dff05e745559e67907bbf21e78reed@google.com        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
3754469938e92d779dff05e745559e67907bbf21e78reed@google.com                                                     SkIntToScalar(kBitmapSize)));
3764469938e92d779dff05e745559e67907bbf21e78reed@google.com        filter = SkRectShaderImageFilter::Create(shader, &cropR);
37722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
37822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
37922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DROP_SHADOW:
380234f036b3e731e06e616c5291157d3bb4fbfdee2sugoi        filter = SkDropShadowImageFilter::Create(make_scalar(), make_scalar(), make_scalar(true),
381234f036b3e731e06e616c5291157d3bb4fbfdee2sugoi                    make_scalar(true), make_color(), make_shadow_mode(), make_image_filter(),
38224e06d5244ae96e440410e1d76e039983b2efac9senorblanco                    NULL);
38322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
38422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MORPHOLOGY:
38543f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        if (R(2) == 1) {
386cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkDilateImageFilter::Create(R(static_cast<float>(kBitmapSize)),
387bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                R(static_cast<float>(kBitmapSize)), make_image_filter());
38843f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        } else {
389cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkErodeImageFilter::Create(R(static_cast<float>(kBitmapSize)),
390bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                R(static_cast<float>(kBitmapSize)), make_image_filter());
39143f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        }
39222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
39322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BITMAP:
39443f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        if (R(2) == 1) {
395cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkBitmapSource::Create(make_bitmap(), make_rect(), make_rect());
39643f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        } else {
397cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkBitmapSource::Create(make_bitmap());
39843f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        }
39922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
40022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DISPLACE:
401cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkDisplacementMapEffect::Create(make_channel_selector_type(),
402cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                 make_channel_selector_type(), make_scalar(),
403cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                 make_image_filter(false), make_image_filter());
40422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
405cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    case TILE:
406cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkTileImageFilter::Create(make_rect(), make_rect(), make_image_filter(false));
407cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        break;
4087b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org    case PICTURE:
4095e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    {
41097f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkRTreeFactory factory;
41197f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkPictureRecorder recorder;
412a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips        SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(kBitmapSize),
413a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                                                            SkIntToScalar(kBitmapSize),
414a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                                                            &factory, 0);
41597f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        drawSomething(recordingCanvas);
41697f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkAutoTUnref<SkPicture> pict(recorder.endRecording());
41797f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        filter = SkPictureImageFilter::Create(pict.get(), make_rect());
4185e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    }
4197b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org        break;
42022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    default:
42122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
42222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
42322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
42422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
42522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
42677e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.orgstatic SkImageFilter* make_serialized_image_filter() {
42777e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkAutoTUnref<SkImageFilter> filter(make_image_filter(false));
42877e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter));
42977e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
43077e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    size_t len = data->size();
43177e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#ifdef SK_ADD_RANDOM_BIT_FLIPS
43277e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    unsigned char* p = const_cast<unsigned char*>(ptr);
43377e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    for (size_t i = 0; i < len; ++i, ++p) {
434cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
435cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            if (R(10) == 1) { // Then 10% of the time, change a whole byte
436cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                switch(R(3)) {
437cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 0:
438cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p ^= 0xFF; // Flip entire byte
439cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
440cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 1:
441cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p = 0xFF; // Set all bits to 1
442cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
443cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 2:
444cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p = 0x00; // Set all bits to 0
445cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
446cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                }
447cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            } else {
448cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                *p ^= (1 << R(8));
449cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            }
45077e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org        }
45177e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    }
45277e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#endif // SK_ADD_RANDOM_BIT_FLIPS
45377e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
45477e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org                                    SkImageFilter::GetFlattenableType());
45577e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    return static_cast<SkImageFilter*>(flattenable);
45677e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org}
45777e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org
458bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.comstatic void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
45922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->save();
46022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
46122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
46222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
46322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->restore();
46422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
46522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
46622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic void do_fuzz(SkCanvas* canvas) {
467ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    SkImageFilter* filter = make_serialized_image_filter();
468ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org
469cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#ifdef SK_FUZZER_IS_VERBOSE
470ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    static uint32_t numFilters = 0;
471ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    static uint32_t numValidFilters = 0;
472ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    if (0 == numFilters) {
473cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        printf("Fuzzing with %u\n", kSeed);
474cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    }
475ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    numFilters++;
47649f085dddff10473b6ebf832a974288300224e60bsalomon    if (filter) {
477ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        numValidFilters++;
478ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    }
479ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
480cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    fflush(stdout);
481cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#endif
482cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org
48322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint paint;
484ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    SkSafeUnref(paint.setImageFilter(filter));
48522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    drawClippedBitmap(canvas, 0, 0, paint);
48622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
48722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
48822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
48922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
49022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgclass ImageFilterFuzzView : public SampleView {
49122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgpublic:
49222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    ImageFilterFuzzView() {
49322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        this->setBGColor(0xFFDDDDDD);
49422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
49522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
49622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgprotected:
49722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    // overrides from SkEventSink
49822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    virtual bool onQuery(SkEvent* evt) {
49922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        if (SampleCode::TitleQ(*evt)) {
50022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            SampleCode::TitleR(evt, "ImageFilterFuzzer");
50122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            return true;
50222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        }
50322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        return this->INHERITED::onQuery(evt);
50422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
50522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
50622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    void drawBG(SkCanvas* canvas) {
50722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        canvas->drawColor(0xFFDDDDDD);
50822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
50922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
51022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    virtual void onDrawContent(SkCanvas* canvas) {
51122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        do_fuzz(canvas);
51222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        this->inval(0);
51322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
51422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
51522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgprivate:
51622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    typedef SkView INHERITED;
51722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org};
51822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
51922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
52022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
52122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkView* MyFactory() { return new ImageFilterFuzzView; }
52222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkViewRegister reg(MyFactory);
523