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