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 | |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 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: |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 20 | ImageLightingGM() { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkISize onISize() override { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 31 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 32 | } |
| 33 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 34 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 35 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 36 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 37 | canvas->clipRect(SkRect::MakeWH( |
| 38 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 39 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 40 | canvas->restore(); |
| 41 | } |
| 42 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 43 | void onOnceBeforeDraw() override { |
| 44 | fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
| 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDraw(SkCanvas* canvas) override { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 48 | canvas->clear(sk_tool_utils::color_to_565(0xFF101010)); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 49 | SkPaint checkPaint; |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 50 | checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020)); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 51 | for (int y = 0; y < HEIGHT; y += 16) { |
| 52 | for (int x = 0; x < WIDTH; x += 16) { |
| 53 | canvas->save(); |
| 54 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 55 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 56 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 57 | canvas->restore(); |
| 58 | } |
| 59 | } |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 60 | SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 61 | SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); |
| 62 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 63 | SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(SkScalarCos(azimuthRad), |
| 64 | SkScalarCos(elevationRad)), |
| 65 | SkScalarMul(SkScalarSin(azimuthRad), |
| 66 | SkScalarCos(elevationRad)), |
| 67 | SkScalarSin(elevationRad)); |
| 68 | SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10), |
| 69 | SkIntToScalar(-10), |
| 70 | SkIntToScalar(20)); |
| 71 | SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0); |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 72 | SkScalar spotExponent = SK_Scalar1; |
| 73 | SkScalar cutoffAngle = SkIntToScalar(15); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 74 | SkScalar kd = SkIntToScalar(2); |
| 75 | SkScalar ks = SkIntToScalar(1); |
| 76 | SkScalar shininess = SkIntToScalar(8); |
| 77 | SkScalar surfaceScale = SkIntToScalar(1); |
| 78 | SkColor white(0xFFFFFFFF); |
| 79 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 80 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 81 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65)); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 82 | SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 83 | SkAutoTUnref<SkImageFilter> noopCropped(SkOffsetImageFilter::Create(0, 0, nullptr, &cropRect)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 84 | |
| 85 | int y = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 86 | for (int i = 0; i < 3; i++) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 87 | const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr; |
| 88 | SkImageFilter* input = (i == 2) ? noopCropped.get() : nullptr; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 89 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, |
| 90 | white, |
| 91 | surfaceScale, |
| 92 | kd, |
| 93 | input, |
| 94 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 95 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 96 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 97 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, |
| 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, 110, 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::CreateSpotLitDiffuse(spotLocation, |
| 106 | spotTarget, |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 107 | spotExponent, |
| 108 | cutoffAngle, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 109 | white, |
| 110 | surfaceScale, |
| 111 | kd, |
| 112 | input, |
| 113 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 114 | drawClippedBitmap(canvas, paint, 220, y); |
| 115 | |
| 116 | y += 110; |
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::CreatePointLitSpecular(pointLocation, |
| 119 | white, |
| 120 | surfaceScale, |
| 121 | ks, |
| 122 | shininess, |
| 123 | input, |
| 124 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 125 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 126 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 127 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, |
| 128 | white, |
| 129 | surfaceScale, |
| 130 | ks, |
| 131 | shininess, |
| 132 | input, |
| 133 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 134 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 135 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 136 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, |
| 137 | spotTarget, |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 138 | spotExponent, |
| 139 | cutoffAngle, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 140 | white, |
| 141 | surfaceScale, |
| 142 | ks, |
| 143 | shininess, |
| 144 | input, |
| 145 | cr))->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 146 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 147 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 148 | y += 110; |
| 149 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 150 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 151 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 152 | private: |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 153 | SkBitmap fBitmap; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 154 | |
| 155 | typedef GM INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | ////////////////////////////////////////////////////////////////////////////// |
| 159 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 160 | DEF_GM(return new ImageLightingGM;) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 161 | } |