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 #include "SkOffsetImageFilter.h"
11 
12 #define WIDTH 330
13 #define HEIGHT 660
14 
15 namespace skiagm {
16 
17 class ImageLightingGM : public GM {
18 public:
ImageLightingGM()19     ImageLightingGM() : fInitialized(false) {
20         this->setBGColor(0xFF000000);
21     }
22 
23 protected:
24 
onShortName()25     SkString onShortName() override {
26         return SkString("lighting");
27     }
28 
make_bitmap()29     void make_bitmap() {
30         fBitmap.allocN32Pixels(100, 100);
31         SkCanvas canvas(fBitmap);
32         canvas.clear(0x00000000);
33         SkPaint paint;
34         paint.setAntiAlias(true);
35         sk_tool_utils::set_portable_typeface(&paint);
36         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 
onISize()42     SkISize onISize() override {
43         return SkISize::Make(WIDTH, HEIGHT);
44     }
45 
drawClippedBitmap(SkCanvas * canvas,const SkPaint & paint,int x,int y)46     void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
47         canvas->save();
48         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);
52         canvas->restore();
53     }
54 
onDraw(SkCanvas * canvas)55     void onDraw(SkCanvas* canvas) override {
56         if (!fInitialized) {
57             make_bitmap();
58             fInitialized = true;
59         }
60         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         }
72         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;
88 
89         SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
90         SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
91         SkAutoTUnref<SkImageFilter> noopCropped(SkOffsetImageFilter::Create(0, 0, NULL, &cropRect));
92 
93         int y = 0;
94         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();
103             drawClippedBitmap(canvas, paint, 0, y);
104 
105             paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection,
106                                                                                 white,
107                                                                                 surfaceScale,
108                                                                                 kd,
109                                                                                 input,
110                                                                                 cr))->unref();
111             drawClippedBitmap(canvas, paint, 110, y);
112 
113             paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation,
114                                                                              spotTarget,
115                                                                              spotExponent,
116                                                                              cutoffAngle,
117                                                                              white,
118                                                                              surfaceScale,
119                                                                              kd,
120                                                                              input,
121                                                                              cr))->unref();
122             drawClippedBitmap(canvas, paint, 220, y);
123 
124             y += 110;
125 
126             paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation,
127                                                                                white,
128                                                                                surfaceScale,
129                                                                                ks,
130                                                                                shininess,
131                                                                                input,
132                                                                                cr))->unref();
133             drawClippedBitmap(canvas, paint, 0, y);
134 
135             paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection,
136                                                                                  white,
137                                                                                  surfaceScale,
138                                                                                  ks,
139                                                                                  shininess,
140                                                                                  input,
141                                                                                  cr))->unref();
142             drawClippedBitmap(canvas, paint, 110, y);
143 
144             paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation,
145                                                                               spotTarget,
146                                                                               spotExponent,
147                                                                               cutoffAngle,
148                                                                               white,
149                                                                               surfaceScale,
150                                                                               ks,
151                                                                               shininess,
152                                                                               input,
153                                                                               cr))->unref();
154             drawClippedBitmap(canvas, paint, 220, y);
155 
156             y += 110;
157         }
158     }
159 
160 private:
161     typedef GM INHERITED;
162     SkBitmap fBitmap;
163     bool fInitialized;
164 };
165 
166 //////////////////////////////////////////////////////////////////////////////
167 
MyFactory(void *)168 static GM* MyFactory(void*) { return new ImageLightingGM; }
169 static GMRegistry reg(MyFactory);
170 
171 }
172