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" |
| 10 | |
| 11 | #define WIDTH 330 |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 12 | #define HEIGHT 440 |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | class ImageLightingGM : public GM { |
| 17 | public: |
| 18 | ImageLightingGM() : fInitialized(false) { |
| 19 | this->setBGColor(0xFF000000); |
| 20 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 21 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 22 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 23 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 24 | SkString onShortName() SK_OVERRIDE { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 25 | return SkString("lighting"); |
| 26 | } |
| 27 | |
| 28 | void make_bitmap() { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 29 | fBitmap.allocN32Pixels(100, 100); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 30 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 31 | canvas.clear(0x00000000); |
| 32 | SkPaint paint; |
| 33 | paint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 34 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 35 | paint.setColor(0xFFFFFFFF); |
| 36 | paint.setTextSize(SkIntToScalar(96)); |
| 37 | const char* str = "e"; |
| 38 | canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); |
| 39 | } |
| 40 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 41 | SkISize onISize() SK_OVERRIDE { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 42 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 43 | } |
| 44 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 45 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 46 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 47 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 48 | canvas->clipRect(SkRect::MakeWH( |
| 49 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 50 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 51 | canvas->restore(); |
| 52 | } |
| 53 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 55 | if (!fInitialized) { |
| 56 | make_bitmap(); |
| 57 | fInitialized = true; |
| 58 | } |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 59 | canvas->clear(0xFF101010); |
| 60 | SkPaint checkPaint; |
| 61 | checkPaint.setColor(0xFF202020); |
| 62 | for (int y = 0; y < HEIGHT; y += 16) { |
| 63 | for (int x = 0; x < WIDTH; x += 16) { |
| 64 | canvas->save(); |
| 65 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 66 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 67 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 68 | canvas->restore(); |
| 69 | } |
| 70 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 71 | SkPoint3 pointLocation(0, 0, SkIntToScalar(10)); |
| 72 | SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); |
| 73 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
| 74 | SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)), |
| 75 | SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)), |
| 76 | SkScalarSin(elevationRad)); |
| 77 | SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20)); |
| 78 | SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 79 | SkScalar spotExponent = SK_Scalar1; |
| 80 | SkScalar cutoffAngle = SkIntToScalar(15); |
| 81 | SkScalar kd = SkIntToScalar(2); |
| 82 | SkScalar ks = SkIntToScalar(1); |
| 83 | SkScalar shininess = SkIntToScalar(8); |
| 84 | SkScalar surfaceScale = SkIntToScalar(1); |
| 85 | SkColor white(0xFFFFFFFF); |
| 86 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 87 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 88 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 89 | |
| 90 | int y = 0; |
| 91 | for (int i = 0; i < 2; i++) { |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 92 | const SkImageFilter::CropRect* cr = (i == 0) ? NULL : &cropRect; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 93 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd, NULL, cr))->unref(); |
| 94 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 95 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 96 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd, NULL, cr))->unref(); |
| 97 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 98 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 99 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, NULL, cr))->unref(); |
| 100 | drawClippedBitmap(canvas, paint, 220, y); |
| 101 | |
| 102 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 103 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 104 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess, NULL, cr))->unref(); |
| 105 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 106 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 107 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess, NULL, cr))->unref(); |
| 108 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 109 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 110 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess, NULL, cr))->unref(); |
| 111 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 112 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 113 | y += 110; |
| 114 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 115 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 117 | private: |
| 118 | typedef GM INHERITED; |
| 119 | SkBitmap fBitmap; |
| 120 | bool fInitialized; |
| 121 | }; |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | static GM* MyFactory(void*) { return new ImageLightingGM; } |
| 126 | static GMRegistry reg(MyFactory); |
| 127 | |
| 128 | } |