senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkLightingImageFilter.h" |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 10 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 11 | |
| 12 | #define WIDTH 330 |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 13 | #define HEIGHT 660 |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class ImageLightingGM : public GM { |
| 18 | public: |
| 19 | ImageLightingGM() : fInitialized(false) { |
| 20 | this->setBGColor(0xFF000000); |
| 21 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 22 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 23 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | SkString onShortName() override { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 26 | return SkString("lighting"); |
| 27 | } |
| 28 | |
| 29 | void make_bitmap() { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 30 | fBitmap.allocN32Pixels(100, 100); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 31 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 32 | canvas.clear(0x00000000); |
| 33 | SkPaint paint; |
| 34 | paint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 35 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 36 | paint.setColor(0xFFFFFFFF); |
| 37 | paint.setTextSize(SkIntToScalar(96)); |
| 38 | const char* str = "e"; |
| 39 | canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); |
| 40 | } |
| 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | SkISize onISize() override { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 43 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 44 | } |
| 45 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 46 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 47 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 48 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 49 | canvas->clipRect(SkRect::MakeWH( |
| 50 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 51 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 52 | canvas->restore(); |
| 53 | } |
| 54 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | void onDraw(SkCanvas* canvas) override { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 56 | if (!fInitialized) { |
| 57 | make_bitmap(); |
| 58 | fInitialized = true; |
| 59 | } |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 60 | canvas->clear(0xFF101010); |
| 61 | SkPaint checkPaint; |
| 62 | checkPaint.setColor(0xFF202020); |
| 63 | for (int y = 0; y < HEIGHT; y += 16) { |
| 64 | for (int x = 0; x < WIDTH; x += 16) { |
| 65 | canvas->save(); |
| 66 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 67 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 68 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 69 | canvas->restore(); |
| 70 | } |
| 71 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 72 | SkPoint3 pointLocation(0, 0, SkIntToScalar(10)); |
| 73 | SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); |
| 74 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
| 75 | SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)), |
| 76 | SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)), |
| 77 | SkScalarSin(elevationRad)); |
| 78 | SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20)); |
| 79 | SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 80 | SkScalar spotExponent = SK_Scalar1; |
| 81 | SkScalar cutoffAngle = SkIntToScalar(15); |
| 82 | SkScalar kd = SkIntToScalar(2); |
| 83 | SkScalar ks = SkIntToScalar(1); |
| 84 | SkScalar shininess = SkIntToScalar(8); |
| 85 | SkScalar surfaceScale = SkIntToScalar(1); |
| 86 | SkColor white(0xFFFFFFFF); |
| 87 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 88 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 89 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65)); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 90 | SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 91 | SkAutoTUnref<SkImageFilter> noopCropped(SkOffsetImageFilter::Create(0, 0, NULL, &cropRect)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 92 | |
| 93 | int y = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 94 | for (int i = 0; i < 3; i++) { |
| 95 | const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : NULL; |
| 96 | SkImageFilter* input = (i == 2) ? noopCropped.get() : NULL; |
| 97 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, |
| 98 | white, |
| 99 | surfaceScale, |
| 100 | kd, |
| 101 | input, |
| 102 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 103 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 104 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 105 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, |
| 106 | white, |
| 107 | surfaceScale, |
| 108 | kd, |
| 109 | input, |
| 110 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 111 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 112 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 113 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, |
| 114 | spotTarget, |
| 115 | spotExponent, |
| 116 | cutoffAngle, |
| 117 | white, |
| 118 | surfaceScale, |
| 119 | kd, |
| 120 | input, |
| 121 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 122 | drawClippedBitmap(canvas, paint, 220, y); |
| 123 | |
| 124 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 125 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 126 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, |
| 127 | white, |
| 128 | surfaceScale, |
| 129 | ks, |
| 130 | shininess, |
| 131 | input, |
| 132 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 133 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 134 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 135 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, |
| 136 | white, |
| 137 | surfaceScale, |
| 138 | ks, |
| 139 | shininess, |
| 140 | input, |
| 141 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 142 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 143 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 144 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, |
| 145 | spotTarget, |
| 146 | spotExponent, |
| 147 | cutoffAngle, |
| 148 | white, |
| 149 | surfaceScale, |
| 150 | ks, |
| 151 | shininess, |
| 152 | input, |
| 153 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 154 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 155 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 156 | y += 110; |
| 157 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 158 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 160 | private: |
| 161 | typedef GM INHERITED; |
| 162 | SkBitmap fBitmap; |
| 163 | bool fInitialized; |
| 164 | }; |
| 165 | |
| 166 | ////////////////////////////////////////////////////////////////////////////// |
| 167 | |
| 168 | static GM* MyFactory(void*) { return new ImageLightingGM; } |
| 169 | static GMRegistry reg(MyFactory); |
| 170 | |
| 171 | } |