blob: 0bfbb47a95e140a4f436a102d43629b171e3555b [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"
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000011
12#define WIDTH 330
senorblancod0d37ca2015-04-02 04:54:56 -070013#define HEIGHT 660
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000014
15namespace skiagm {
16
17class ImageLightingGM : public GM {
18public:
19 ImageLightingGM() : fInitialized(false) {
20 this->setBGColor(0xFF000000);
21 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000023protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000024
mtklein36352bf2015-03-25 18:17:31 -070025 SkString onShortName() override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000026 return SkString("lighting");
27 }
28
29 void make_bitmap() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000030 fBitmap.allocN32Pixels(100, 100);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000031 SkCanvas canvas(fBitmap);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000032 canvas.clear(0x00000000);
33 SkPaint paint;
34 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040035 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000036 paint.setColor(0xFFFFFFFF);
37 paint.setTextSize(SkIntToScalar(96));
38 const char* str = "e";
39 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000043 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000044 }
45
junov@chromium.orgff06af22013-01-14 16:27:50 +000046 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
47 canvas->save();
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000048 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
49 canvas->clipRect(SkRect::MakeWH(
50 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
51 canvas->drawBitmap(fBitmap, 0, 0, &paint);
junov@chromium.orgff06af22013-01-14 16:27:50 +000052 canvas->restore();
53 }
54
mtklein36352bf2015-03-25 18:17:31 -070055 void onDraw(SkCanvas* canvas) override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000056 if (!fInitialized) {
57 make_bitmap();
58 fInitialized = true;
59 }
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000060 canvas->clear(0xFF101010);
61 SkPaint checkPaint;
62 checkPaint.setColor(0xFF202020);
63 for (int y = 0; y < HEIGHT; y += 16) {
64 for (int x = 0; x < WIDTH; x += 16) {
65 canvas->save();
66 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
67 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
68 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
69 canvas->restore();
70 }
71 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000072 SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
73 SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
74 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
75 SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)),
76 SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)),
77 SkScalarSin(elevationRad));
78 SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
79 SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
80 SkScalar spotExponent = SK_Scalar1;
81 SkScalar cutoffAngle = SkIntToScalar(15);
82 SkScalar kd = SkIntToScalar(2);
83 SkScalar ks = SkIntToScalar(1);
84 SkScalar shininess = SkIntToScalar(8);
85 SkScalar surfaceScale = SkIntToScalar(1);
86 SkColor white(0xFFFFFFFF);
87 SkPaint paint;
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000088
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000089 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
senorblancod0d37ca2015-04-02 04:54:56 -070090 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
91 SkAutoTUnref<SkImageFilter> noopCropped(SkOffsetImageFilter::Create(0, 0, NULL, &cropRect));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000092
93 int y = 0;
senorblancod0d37ca2015-04-02 04:54:56 -070094 for (int i = 0; i < 3; i++) {
95 const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : NULL;
96 SkImageFilter* input = (i == 2) ? noopCropped.get() : NULL;
97 paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation,
98 white,
99 surfaceScale,
100 kd,
101 input,
102 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000103 drawClippedBitmap(canvas, paint, 0, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000104
senorblancod0d37ca2015-04-02 04:54:56 -0700105 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection,
106 white,
107 surfaceScale,
108 kd,
109 input,
110 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000111 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000112
senorblancod0d37ca2015-04-02 04:54:56 -0700113 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation,
114 spotTarget,
115 spotExponent,
116 cutoffAngle,
117 white,
118 surfaceScale,
119 kd,
120 input,
121 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000122 drawClippedBitmap(canvas, paint, 220, y);
123
124 y += 110;
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000125
senorblancod0d37ca2015-04-02 04:54:56 -0700126 paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation,
127 white,
128 surfaceScale,
129 ks,
130 shininess,
131 input,
132 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000133 drawClippedBitmap(canvas, paint, 0, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000134
senorblancod0d37ca2015-04-02 04:54:56 -0700135 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection,
136 white,
137 surfaceScale,
138 ks,
139 shininess,
140 input,
141 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000142 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000143
senorblancod0d37ca2015-04-02 04:54:56 -0700144 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation,
145 spotTarget,
146 spotExponent,
147 cutoffAngle,
148 white,
149 surfaceScale,
150 ks,
151 shininess,
152 input,
153 cr))->unref();
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000154 drawClippedBitmap(canvas, paint, 220, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000155
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000156 y += 110;
157 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000160private:
161 typedef GM INHERITED;
162 SkBitmap fBitmap;
163 bool fInitialized;
164};
165
166//////////////////////////////////////////////////////////////////////////////
167
168static GM* MyFactory(void*) { return new ImageLightingGM; }
169static GMRegistry reg(MyFactory);
170
171}