blob: f6d236c382cda1bdfea07565f2e6b92aad3cba7a [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:
20 ImageLightingGM() : fInitialized(false) {
21 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
30 void make_bitmap() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000031 fBitmap.allocN32Pixels(100, 100);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000032 SkCanvas canvas(fBitmap);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000033 canvas.clear(0x00000000);
34 SkPaint paint;
35 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070036 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000037 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
mtklein36352bf2015-03-25 18:17:31 -070043 SkISize onISize() override {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000044 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000045 }
46
junov@chromium.orgff06af22013-01-14 16:27:50 +000047 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
48 canvas->save();
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000049 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.orgff06af22013-01-14 16:27:50 +000053 canvas->restore();
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 void onDraw(SkCanvas* canvas) override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000057 if (!fInitialized) {
58 make_bitmap();
59 fInitialized = true;
60 }
caryclark12596012015-07-29 05:27:47 -070061 canvas->clear(sk_tool_utils::color_to_565(0xFF101010));
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000062 SkPaint checkPaint;
caryclark12596012015-07-29 05:27:47 -070063 checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020));
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000064 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 }
robertphillips3d32d762015-07-13 13:16:44 -070073 SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000074 SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
75 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
robertphillips3d32d762015-07-13 13:16:44 -070076 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.orgf49b4292012-06-22 21:01:23 +000085 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.org4e16bb22013-07-26 00:10:07 +000093
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000094 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
senorblancod0d37ca2015-04-02 04:54:56 -070095 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
96 SkAutoTUnref<SkImageFilter> noopCropped(SkOffsetImageFilter::Create(0, 0, NULL, &cropRect));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000097
98 int y = 0;
senorblancod0d37ca2015-04-02 04:54:56 -070099 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.org4e16bb22013-07-26 00:10:07 +0000108 drawClippedBitmap(canvas, paint, 0, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000109
senorblancod0d37ca2015-04-02 04:54:56 -0700110 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection,
111 white,
112 surfaceScale,
113 kd,
114 input,
115 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000116 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000117
senorblancod0d37ca2015-04-02 04:54:56 -0700118 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation,
119 spotTarget,
120 spotExponent,
121 cutoffAngle,
122 white,
123 surfaceScale,
124 kd,
125 input,
126 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000127 drawClippedBitmap(canvas, paint, 220, y);
128
129 y += 110;
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000130
senorblancod0d37ca2015-04-02 04:54:56 -0700131 paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation,
132 white,
133 surfaceScale,
134 ks,
135 shininess,
136 input,
137 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000138 drawClippedBitmap(canvas, paint, 0, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000139
senorblancod0d37ca2015-04-02 04:54:56 -0700140 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection,
141 white,
142 surfaceScale,
143 ks,
144 shininess,
145 input,
146 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000147 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000148
senorblancod0d37ca2015-04-02 04:54:56 -0700149 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.org4e16bb22013-07-26 00:10:07 +0000159 drawClippedBitmap(canvas, paint, 220, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000160
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000161 y += 110;
162 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000165private:
166 typedef GM INHERITED;
167 SkBitmap fBitmap;
168 bool fInitialized;
169};
170
171//////////////////////////////////////////////////////////////////////////////
172
robertphillips5f865b92015-07-29 12:28:04 -0700173DEF_GM( return SkNEW(ImageLightingGM); )
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000174
175}