blob: af102d7d03950f712c0a285b9303f7f3dc381ded [file] [log] [blame]
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +00001/*
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"
senorblancod0d37ca2015-04-02 04:54:56 -070010#include "SkOffsetImageFilter.h"
robertphillips3d32d762015-07-13 13:16:44 -070011#include "SkPoint3.h"
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000012
13#define WIDTH 330
senorblancod0d37ca2015-04-02 04:54:56 -070014#define HEIGHT 660
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000015
16namespace skiagm {
17
18class ImageLightingGM : public GM {
19public:
robertphillips943a4622015-09-03 13:32:33 -070020 ImageLightingGM() {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000021 this->setBGColor(0xFF000000);
22 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000024protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000025
mtklein36352bf2015-03-25 18:17:31 -070026 SkString onShortName() override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000027 return SkString("lighting");
28 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkISize onISize() override {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000031 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000032 }
33
junov@chromium.orgff06af22013-01-14 16:27:50 +000034 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
35 canvas->save();
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000036 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.orgff06af22013-01-14 16:27:50 +000040 canvas->restore();
41 }
42
robertphillips943a4622015-09-03 13:32:33 -070043 void onOnceBeforeDraw() override {
44 fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
45 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 void onDraw(SkCanvas* canvas) override {
caryclark12596012015-07-29 05:27:47 -070048 canvas->clear(sk_tool_utils::color_to_565(0xFF101010));
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000049 SkPaint checkPaint;
caryclark12596012015-07-29 05:27:47 -070050 checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020));
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000051 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 }
robertphillips3d32d762015-07-13 13:16:44 -070060 SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000061 SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
62 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
robertphillips3d32d762015-07-13 13:16:44 -070063 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);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000072 SkScalar spotExponent = SK_Scalar1;
73 SkScalar cutoffAngle = SkIntToScalar(15);
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.org4e16bb22013-07-26 00:10:07 +000080
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000081 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
senorblancod0d37ca2015-04-02 04:54:56 -070082 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
halcanary96fcdcc2015-08-27 07:41:13 -070083 SkAutoTUnref<SkImageFilter> noopCropped(SkOffsetImageFilter::Create(0, 0, nullptr, &cropRect));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000084
85 int y = 0;
senorblancod0d37ca2015-04-02 04:54:56 -070086 for (int i = 0; i < 3; i++) {
halcanary96fcdcc2015-08-27 07:41:13 -070087 const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr;
88 SkImageFilter* input = (i == 2) ? noopCropped.get() : nullptr;
senorblancod0d37ca2015-04-02 04:54:56 -070089 paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation,
90 white,
91 surfaceScale,
92 kd,
93 input,
94 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000095 drawClippedBitmap(canvas, paint, 0, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000096
senorblancod0d37ca2015-04-02 04:54:56 -070097 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection,
98 white,
99 surfaceScale,
100 kd,
101 input,
102 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000103 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000104
senorblancod0d37ca2015-04-02 04:54:56 -0700105 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation,
106 spotTarget,
107 spotExponent,
108 cutoffAngle,
109 white,
110 surfaceScale,
111 kd,
112 input,
113 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000114 drawClippedBitmap(canvas, paint, 220, y);
115
116 y += 110;
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000117
senorblancod0d37ca2015-04-02 04:54:56 -0700118 paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation,
119 white,
120 surfaceScale,
121 ks,
122 shininess,
123 input,
124 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000125 drawClippedBitmap(canvas, paint, 0, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000126
senorblancod0d37ca2015-04-02 04:54:56 -0700127 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection,
128 white,
129 surfaceScale,
130 ks,
131 shininess,
132 input,
133 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000134 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000135
senorblancod0d37ca2015-04-02 04:54:56 -0700136 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation,
137 spotTarget,
138 spotExponent,
139 cutoffAngle,
140 white,
141 surfaceScale,
142 ks,
143 shininess,
144 input,
145 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000146 drawClippedBitmap(canvas, paint, 220, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000147
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000148 y += 110;
149 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000150 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000152private:
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000153 SkBitmap fBitmap;
robertphillips943a4622015-09-03 13:32:33 -0700154
155 typedef GM INHERITED;
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000156};
157
158//////////////////////////////////////////////////////////////////////////////
159
halcanary385fe4d2015-08-26 13:07:48 -0700160DEF_GM(return new ImageLightingGM;)
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000161}