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 "SkBicubicImageFilter.h"
1077e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#include "SkBitmapDevice.h"
1122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkBitmapSource.h"
1222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkBlurImageFilter.h"
1322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkCanvas.h"
1422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkColorFilter.h"
1522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkColorFilterImageFilter.h"
1622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkComposeImageFilter.h"
1777e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#include "SkData.h"
1822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkDisplacementMapEffect.h"
1922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkDropShadowImageFilter.h"
2022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkFlattenableSerialization.h"
2122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkLightingImageFilter.h"
2222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMagnifierImageFilter.h"
23067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkMatrixImageFilter.h"
24067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkMatrixConvolutionImageFilter.h"
2522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMergeImageFilter.h"
2622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkMorphologyImageFilter.h"
2722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkOffsetImageFilter.h"
2822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkPerlinNoiseShader.h"
297b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org#include "SkPictureImageFilter.h"
3097f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org#include "SkPictureRecorder.h"
3122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkRandom.h"
3222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkRectShaderImageFilter.h"
33067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org#include "SkTestImageFilters.h"
34cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#include "SkTileImageFilter.h"
3522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkView.h"
3622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#include "SkXfermodeImageFilter.h"
37cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#include <stdio.h>
38cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#include <time.h>
3922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
40c2e9db30d393862bd3485cfe57b4ac06433f2f32commit-bot@chromium.org//#define SK_ADD_RANDOM_BIT_FLIPS
41c2e9db30d393862bd3485cfe57b4ac06433f2f32commit-bot@chromium.org//#define SK_FUZZER_IS_VERBOSE
42c2e9db30d393862bd3485cfe57b4ac06433f2f32commit-bot@chromium.org
43cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.orgstatic const uint32_t kSeed = (uint32_t)(time(NULL));
4422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkRandom gRand(kSeed);
4522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic bool return_large = false;
4622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic bool return_undef = false;
4722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
4822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic const int kBitmapSize = 24;
4922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
5022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic int R(float x) {
5122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
5222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
5322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
5422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#if defined _WIN32
5522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#pragma warning ( push )
5622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org// we are intentionally causing an overflow here
5722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//      (warning C4756: overflow in constant arithmetic)
5822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#pragma warning ( disable : 4756 )
5922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#endif
6022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
6122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic float huge() {
6222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    double d = 1e100;
6322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    float f = (float)d;
6422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return f;
6522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
6622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
6722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#if defined _WIN32
6822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#pragma warning ( pop )
6922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org#endif
7022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
7122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic float make_number(bool positiveOnly) {
7222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    float f = positiveOnly ? 1.0f : 0.0f;
7322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    float v = f;
7422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    int sel;
7522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
7622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (return_large) sel = R(6); else sel = R(4);
7722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!return_undef && sel == 0) sel = 1;
7822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
7922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (R(2) == 1) v = (float)(R(100)+f); else
8022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
8122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    switch (sel) {
8222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 0: break;
8322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 1: v = f; break;
8422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 2: v = 0.000001f; break;
8522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 3: v = 10000.0f; break;
8622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 4: v = 2000000000.0f; break;
8722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        case 5: v = huge(); break;
8822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
8922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
9022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!positiveOnly && (R(4) == 1)) v = -v;
9122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return v;
9222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
9322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
9422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkScalar make_scalar(bool positiveOnly = false) {
954b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    return make_number(positiveOnly);
9622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
9722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
98cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.orgstatic SkRect make_rect() {
99cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))),
100cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                          SkIntToScalar(R(static_cast<float>(kBitmapSize))));
10122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
10222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
103067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.orgstatic SkRegion make_region() {
104b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org    SkIRect iRegion = SkIRect::MakeXYWH(R(static_cast<float>(kBitmapSize)),
105b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                        R(static_cast<float>(kBitmapSize)),
106b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                        R(static_cast<float>(kBitmapSize)),
107b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                        R(static_cast<float>(kBitmapSize)));
108067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    return SkRegion(iRegion);
109067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org}
110067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org
111067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.orgstatic SkMatrix make_matrix() {
112067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    SkMatrix m;
113067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    for (int i = 0; i < 9; ++i) {
114067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        m[i] = make_scalar();
115067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    }
116067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    return m;
117067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org}
118067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org
11922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkXfermode::Mode make_xfermode() {
12022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1));
12122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
12222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
12322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkColor make_color() {
12422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090;
12522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
12622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
12722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkPoint3 make_point() {
12822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return SkPoint3(make_scalar(), make_scalar(), make_scalar(true));
12922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
13022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
13122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkDisplacementMapEffect::ChannelSelectorType make_channel_selector_type() {
13222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return static_cast<SkDisplacementMapEffect::ChannelSelectorType>(R(4)+1);
13322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
13422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
135a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic bool valid_for_raster_canvas(const SkImageInfo& info) {
136a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    switch (info.colorType()) {
137a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        case kAlpha_8_SkColorType:
138a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        case kRGB_565_SkColorType:
139a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            return true;
14028fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        case kN32_SkColorType:
141a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            return kPremul_SkAlphaType == info.alphaType() ||
142a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                   kOpaque_SkAlphaType == info.alphaType();
143a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        default:
144a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org            break;
145ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    }
146a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    return false;
147a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
148a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
149a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic SkColorType rand_colortype() {
150a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    return (SkColorType)R(kLastEnum_SkColorType + 1);
151a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
152a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
153a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic void rand_bitmap_for_canvas(SkBitmap* bitmap) {
154a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkImageInfo info;
155a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    do {
156a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org        info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
157a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                                 kPremul_SkAlphaType);
158a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    } while (!valid_for_raster_canvas(info) || !bitmap->allocPixels(info));
159a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org}
160a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
161a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic void make_g_bitmap(SkBitmap& bitmap) {
162a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    rand_bitmap_for_canvas(&bitmap);
163a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
164a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkCanvas canvas(bitmap);
16522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.clear(0x00000000);
16622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint paint;
16722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setAntiAlias(true);
16822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setColor(0xFF884422);
16922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    paint.setTextSize(SkIntToScalar(kBitmapSize/2));
17022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const char* str = "g";
17122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
17222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                    SkIntToScalar(kBitmapSize/4), paint);
17322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
17422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
17522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic void make_checkerboard_bitmap(SkBitmap& bitmap) {
176a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    rand_bitmap_for_canvas(&bitmap);
177a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org
178a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org    SkCanvas canvas(bitmap);
17922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas.clear(0x00000000);
18022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint darkPaint;
18122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    darkPaint.setColor(0xFF804020);
18222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint lightPaint;
18322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    lightPaint.setColor(0xFF244484);
18422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const int i = kBitmapSize / 8;
18522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    const SkScalar f = SkIntToScalar(i);
18622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    for (int y = 0; y < kBitmapSize; y += i) {
18722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        for (int x = 0; x < kBitmapSize; x += i) {
18822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.save();
18922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
19022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
19122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
19222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
19322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
19422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            canvas.restore();
19522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        }
19622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
19722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
19822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
19922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic const SkBitmap& make_bitmap() {
20022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    static SkBitmap bitmap[2];
20122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    static bool initialized = false;
20222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (!initialized) {
20322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        make_g_bitmap(bitmap[0]);
20422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        make_checkerboard_bitmap(bitmap[1]);
20522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        initialized = true;
20622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
20722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return bitmap[R(2)];
20822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
20922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
2105e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.orgstatic void drawSomething(SkCanvas* canvas) {
2115e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    SkPaint paint;
2125e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2135e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->save();
2145e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->scale(0.5f, 0.5f);
2155e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawBitmap(make_bitmap(), 0, 0, NULL);
2165e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->restore();
2175e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2185e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    const char beforeStr[] = "before circle";
2195e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    const char afterStr[] = "after circle";
2205e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2215e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setAntiAlias(true);
2225e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
2235e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setColor(SK_ColorRED);
2245e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawData(beforeStr, sizeof(beforeStr));
2255e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
2265e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawData(afterStr, sizeof(afterStr));
2275e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setColor(SK_ColorBLACK);
2285e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    paint.setTextSize(SkIntToScalar(kBitmapSize/3));
2295e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
2305e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org}
2315e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org
23222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkImageFilter* make_image_filter(bool canBeNull = true) {
23322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkImageFilter* filter = 0;
23422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
23522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    // Add a 1 in 3 chance to get a NULL input
23622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    if (canBeNull && (R(3) == 1)) { return filter; }
23722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
238067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    enum { ALPHA_THRESHOLD, BICUBIC, MERGE, COLOR, BLUR, MAGNIFIER,
239067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org           DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE,
24022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org           DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW,
2417b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org           MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, NUM_FILTERS };
24222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
24322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    switch (R(NUM_FILTERS)) {
244067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case ALPHA_THRESHOLD:
245067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkAlphaThresholdFilter::Create(make_region(), make_scalar(), make_scalar());
246067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
24722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BICUBIC:
24822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        // Scale is set to 1 here so that it can fit in the DAG without resizing the output
24922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = SkBicubicImageFilter::CreateMitchell(SkSize::Make(1, 1), make_image_filter());
25022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
25122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MERGE:
252cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkMergeImageFilter::Create(make_image_filter(), make_image_filter(), make_xfermode());
25322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
25422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case COLOR:
25522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
25622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ?
25722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) :
25822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkColorFilter::CreateLightingFilter(make_color(), make_color()));
25922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0;
26022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
26122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
26222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BLUR:
263cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkBlurImageFilter::Create(make_scalar(true), make_scalar(true), make_image_filter());
26422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
26522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MAGNIFIER:
266cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkMagnifierImageFilter::Create(make_rect(), make_scalar(true));
26722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
268067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case DOWN_SAMPLE:
269067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkDownSampleImageFilter::Create(make_scalar());
270067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
27122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case XFERMODE:
27222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
27322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(make_xfermode()));
274cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkXfermodeImageFilter::Create(mode, make_image_filter(), make_image_filter());
27522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
27622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
27722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case OFFSET:
278cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkOffsetImageFilter::Create(make_scalar(), make_scalar(), make_image_filter());
27922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
280067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case MATRIX:
281067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkMatrixImageFilter::Create(make_matrix(),
282067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                             (SkPaint::FilterLevel)R(4),
283067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                             make_image_filter());
284067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
285067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    case MATRIX_CONVOLUTION:
286067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    {
287067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
288067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                     SkIntToScalar(kBitmapSize)));
289067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkISize size = SkISize::Make(R(10)+1, R(10)+1);
290067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        int arraySize = size.width() * size.height();
291067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        SkTArray<SkScalar> kernel(arraySize);
292067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        for (int i = 0; i < arraySize; ++i) {
293067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org            kernel.push_back() = make_scalar();
294067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        }
295b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org        SkIPoint kernelOffset = SkIPoint::Make(R(SkIntToScalar(size.width())),
296b859fe653003862ca8603a60d561a053f752cc77commit-bot@chromium.org                                               R(SkIntToScalar(size.height())));
297067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        filter = SkMatrixConvolutionImageFilter::Create(size,
298067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        kernel.begin(),
299067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_scalar(),
300067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_scalar(),
301067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        kernelOffset,
302067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        (SkMatrixConvolutionImageFilter::TileMode)R(3),
303067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        R(2) == 1,
304067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        make_image_filter(),
305067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org                                                        &cropR);
306067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org    }
307067fe482663de257c7c4e2887462269813ced255commit-bot@chromium.org        break;
30822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case COMPOSE:
309cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkComposeImageFilter::Create(make_image_filter(), make_image_filter());
31022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
31122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DISTANT_LIGHT:
31222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
31322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateDistantLitDiffuse(make_point(),
31422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
31522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateDistantLitSpecular(make_point(),
31622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
31722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_image_filter());
31822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
31922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case POINT_LIGHT:
32022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
32122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreatePointLitDiffuse(make_point(),
32222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), make_image_filter()) :
32322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreatePointLitSpecular(make_point(),
32422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_color(), make_scalar(), make_scalar(), SkIntToScalar(R(10)),
32522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_image_filter());
32622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
32722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case SPOT_LIGHT:
32822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        filter = (R(2) == 1) ?
32922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateSpotLitDiffuse(SkPoint3(0, 0, 0),
33022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_point(), make_scalar(), make_scalar(), make_color(),
33122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_scalar(), make_scalar(), make_image_filter()) :
33222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 SkLightingImageFilter::CreateSpotLitSpecular(SkPoint3(0, 0, 0),
33322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_point(), make_scalar(), make_scalar(), make_color(),
33422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                 make_scalar(), make_scalar(), SkIntToScalar(R(10)), make_image_filter());
33522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
33622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case NOISE:
33722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    {
33822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkAutoTUnref<SkShader> shader((R(2) == 1) ?
33922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            SkPerlinNoiseShader::CreateFractalNoise(
340bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) :
3419fbbcca1c958e6df2cff24d3ccdb7ebd89b8486bcommit-bot@chromium.org            SkPerlinNoiseShader::CreateTurbulence(
342bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                make_scalar(true), make_scalar(true), R(10.0f), make_scalar()));
3434469938e92d779dff05e745559e67907bbf21e78reed@google.com        SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize),
3444469938e92d779dff05e745559e67907bbf21e78reed@google.com                                                     SkIntToScalar(kBitmapSize)));
3454469938e92d779dff05e745559e67907bbf21e78reed@google.com        filter = SkRectShaderImageFilter::Create(shader, &cropR);
34622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
34722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
34822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DROP_SHADOW:
349cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkDropShadowImageFilter::Create(make_scalar(), make_scalar(),
35022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org                     make_scalar(true), make_color(), make_image_filter());
35122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
35222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case MORPHOLOGY:
35343f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        if (R(2) == 1) {
354cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkDilateImageFilter::Create(R(static_cast<float>(kBitmapSize)),
355bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                R(static_cast<float>(kBitmapSize)), make_image_filter());
35643f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        } else {
357cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkErodeImageFilter::Create(R(static_cast<float>(kBitmapSize)),
358bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.com                R(static_cast<float>(kBitmapSize)), make_image_filter());
35943f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        }
36022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
36122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case BITMAP:
36243f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        if (R(2) == 1) {
363cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkBitmapSource::Create(make_bitmap(), make_rect(), make_rect());
36443f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        } else {
365cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org            filter = SkBitmapSource::Create(make_bitmap());
36643f4a55104fff93157c396dad7224ab6fd9547dfcommit-bot@chromium.org        }
36722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
36822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    case DISPLACE:
369cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkDisplacementMapEffect::Create(make_channel_selector_type(),
370cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                 make_channel_selector_type(), make_scalar(),
371cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                 make_image_filter(false), make_image_filter());
37222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
373cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    case TILE:
374cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org        filter = SkTileImageFilter::Create(make_rect(), make_rect(), make_image_filter(false));
375cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        break;
3767b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org    case PICTURE:
3775e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    {
37897f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkRTreeFactory factory;
37997f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkPictureRecorder recorder;
38097f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkCanvas* recordingCanvas = recorder.beginRecording(kBitmapSize, kBitmapSize, &factory, 0);
38197f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        drawSomething(recordingCanvas);
38297f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        SkAutoTUnref<SkPicture> pict(recorder.endRecording());
38397f5fc651956287e78e35934cf62b9e1b45b4f6csenorblanco@chromium.org        filter = SkPictureImageFilter::Create(pict.get(), make_rect());
3845e0995e4b36178e1e4465a9f50114ed39f038c27commit-bot@chromium.org    }
3857b8d72d8922d91f43d84d6c11b033157c7ba6576commit-bot@chromium.org        break;
38622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    default:
38722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        break;
38822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
38922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    return (filter || canBeNull) ? filter : make_image_filter(canBeNull);
39022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
39122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
39277e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.orgstatic SkImageFilter* make_serialized_image_filter() {
39377e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkAutoTUnref<SkImageFilter> filter(make_image_filter(false));
39477e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter));
39577e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    const unsigned char* ptr = static_cast<const unsigned char*>(data->data());
39677e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    size_t len = data->size();
39777e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#ifdef SK_ADD_RANDOM_BIT_FLIPS
39877e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    unsigned char* p = const_cast<unsigned char*>(ptr);
39977e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    for (size_t i = 0; i < len; ++i, ++p) {
400cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        if (R(250) == 1) { // 0.4% of the time, flip a bit or byte
401cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            if (R(10) == 1) { // Then 10% of the time, change a whole byte
402cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                switch(R(3)) {
403cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 0:
404cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p ^= 0xFF; // Flip entire byte
405cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
406cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 1:
407cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p = 0xFF; // Set all bits to 1
408cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
409cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                case 2:
410cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    *p = 0x00; // Set all bits to 0
411cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                    break;
412cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                }
413cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            } else {
414cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org                *p ^= (1 << R(8));
415cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org            }
41677e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org        }
41777e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    }
41877e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org#endif // SK_ADD_RANDOM_BIT_FLIPS
41977e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len,
42077e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org                                    SkImageFilter::GetFlattenableType());
42177e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org    return static_cast<SkImageFilter*>(flattenable);
42277e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org}
42377e079af1a909ee9d14306db48561d77e2f9fcabcommit-bot@chromium.org
424bb8148b7637faafee53085dfaea9c6ece0fa1cc7sugoi@google.comstatic void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
42522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->save();
42622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
42722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
42822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->drawBitmap(make_bitmap(), SkIntToScalar(x), SkIntToScalar(y), &paint);
42922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    canvas->restore();
43022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
43122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
43222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic void do_fuzz(SkCanvas* canvas) {
433ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    SkImageFilter* filter = make_serialized_image_filter();
434ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org
435cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#ifdef SK_FUZZER_IS_VERBOSE
436ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    static uint32_t numFilters = 0;
437ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    static uint32_t numValidFilters = 0;
438ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    if (0 == numFilters) {
439cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        printf("Fuzzing with %u\n", kSeed);
440cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    }
441ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    numFilters++;
442ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    if (NULL != filter) {
443ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        numValidFilters++;
444ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    }
445ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    printf("Filter no : %u. Valid filters so far : %u\r", numFilters, numValidFilters);
446cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    fflush(stdout);
447cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org#endif
448cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org
44922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    SkPaint paint;
450ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org    SkSafeUnref(paint.setImageFilter(filter));
45122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    drawClippedBitmap(canvas, 0, 0, paint);
45222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org}
45322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
45422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
45522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
45622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgclass ImageFilterFuzzView : public SampleView {
45722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgpublic:
45822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    ImageFilterFuzzView() {
45922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        this->setBGColor(0xFFDDDDDD);
46022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
46122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
46222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgprotected:
46322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    // overrides from SkEventSink
46422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    virtual bool onQuery(SkEvent* evt) {
46522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        if (SampleCode::TitleQ(*evt)) {
46622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            SampleCode::TitleR(evt, "ImageFilterFuzzer");
46722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org            return true;
46822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        }
46922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        return this->INHERITED::onQuery(evt);
47022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
47122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
47222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    void drawBG(SkCanvas* canvas) {
47322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        canvas->drawColor(0xFFDDDDDD);
47422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
47522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
47622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    virtual void onDrawContent(SkCanvas* canvas) {
47722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        do_fuzz(canvas);
47822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org        this->inval(0);
47922d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    }
48022d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
48122d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgprivate:
48222d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org    typedef SkView INHERITED;
48322d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org};
48422d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
48522d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
48622d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.org
48722d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkView* MyFactory() { return new ImageFilterFuzzView; }
48822d69f9973a7aa942b3ecb91931fbed4b52ecb98commit-bot@chromium.orgstatic SkViewRegister reg(MyFactory);
489