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 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 24 | return kSkipTiled_Flag; |
| 25 | } |
| 26 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 27 | virtual SkString onShortName() { |
| 28 | return SkString("lighting"); |
| 29 | } |
| 30 | |
| 31 | void make_bitmap() { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 32 | fBitmap.allocN32Pixels(100, 100); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 33 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 34 | canvas.clear(0x00000000); |
| 35 | SkPaint paint; |
| 36 | paint.setAntiAlias(true); |
| 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 | |
| 43 | virtual SkISize onISize() { |
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 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 56 | virtual void onDraw(SkCanvas* canvas) { |
| 57 | if (!fInitialized) { |
| 58 | make_bitmap(); |
| 59 | fInitialized = true; |
| 60 | } |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 61 | canvas->clear(0xFF101010); |
| 62 | SkPaint checkPaint; |
| 63 | checkPaint.setColor(0xFF202020); |
| 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 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 73 | SkPoint3 pointLocation(0, 0, SkIntToScalar(10)); |
| 74 | SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); |
| 75 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
| 76 | SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)), |
| 77 | SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)), |
| 78 | SkScalarSin(elevationRad)); |
| 79 | SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20)); |
| 80 | SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 81 | SkScalar spotExponent = SK_Scalar1; |
| 82 | SkScalar cutoffAngle = SkIntToScalar(15); |
| 83 | SkScalar kd = SkIntToScalar(2); |
| 84 | SkScalar ks = SkIntToScalar(1); |
| 85 | SkScalar shininess = SkIntToScalar(8); |
| 86 | SkScalar surfaceScale = SkIntToScalar(1); |
| 87 | SkColor white(0xFFFFFFFF); |
| 88 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 89 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 90 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 91 | |
| 92 | int y = 0; |
| 93 | for (int i = 0; i < 2; i++) { |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 94 | const SkImageFilter::CropRect* cr = (i == 0) ? NULL : &cropRect; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 95 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd, NULL, cr))->unref(); |
| 96 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 97 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 98 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd, NULL, cr))->unref(); |
| 99 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 100 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 101 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, NULL, cr))->unref(); |
| 102 | drawClippedBitmap(canvas, paint, 220, y); |
| 103 | |
| 104 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 105 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 106 | paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess, NULL, cr))->unref(); |
| 107 | drawClippedBitmap(canvas, paint, 0, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 108 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 109 | paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess, NULL, cr))->unref(); |
| 110 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 111 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 112 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess, NULL, cr))->unref(); |
| 113 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 114 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 115 | y += 110; |
| 116 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 117 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 118 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 119 | private: |
| 120 | typedef GM INHERITED; |
| 121 | SkBitmap fBitmap; |
| 122 | bool fInitialized; |
| 123 | }; |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | static GM* MyFactory(void*) { return new ImageLightingGM; } |
| 128 | static GMRegistry reg(MyFactory); |
| 129 | |
| 130 | } |