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
12 #define HEIGHT 440
13
14 namespace skiagm {
15
16 class ImageLightingGM : public GM {
17 public:
ImageLightingGM()18 ImageLightingGM() : fInitialized(false) {
19 this->setBGColor(0xFF000000);
20 }
21
22 protected:
onShortName()23 virtual SkString onShortName() {
24 return SkString("lighting");
25 }
26
make_bitmap()27 void make_bitmap() {
28 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
29 fBitmap.allocPixels();
30 SkBitmapDevice device(fBitmap);
31 SkCanvas canvas(&device);
32 canvas.clear(0x00000000);
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setColor(0xFFFFFFFF);
36 paint.setTextSize(SkIntToScalar(96));
37 const char* str = "e";
38 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
39 }
40
onISize()41 virtual SkISize onISize() {
42 return make_isize(WIDTH, HEIGHT);
43 }
44
drawClippedBitmap(SkCanvas * canvas,const SkPaint & paint,int x,int y)45 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
46 canvas->save();
47 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
48 canvas->clipRect(SkRect::MakeWH(
49 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
50 canvas->drawBitmap(fBitmap, 0, 0, &paint);
51 canvas->restore();
52 }
53
onDraw(SkCanvas * canvas)54 virtual void onDraw(SkCanvas* canvas) {
55 if (!fInitialized) {
56 make_bitmap();
57 fInitialized = true;
58 }
59 canvas->clear(0xFF101010);
60 SkPaint checkPaint;
61 checkPaint.setColor(0xFF202020);
62 for (int y = 0; y < HEIGHT; y += 16) {
63 for (int x = 0; x < WIDTH; x += 16) {
64 canvas->save();
65 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
66 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
67 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
68 canvas->restore();
69 }
70 }
71 SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
72 SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
73 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
74 SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)),
75 SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)),
76 SkScalarSin(elevationRad));
77 SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
78 SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
79 SkScalar spotExponent = SK_Scalar1;
80 SkScalar cutoffAngle = SkIntToScalar(15);
81 SkScalar kd = SkIntToScalar(2);
82 SkScalar ks = SkIntToScalar(1);
83 SkScalar shininess = SkIntToScalar(8);
84 SkScalar surfaceScale = SkIntToScalar(1);
85 SkColor white(0xFFFFFFFF);
86 SkPaint paint;
87
88 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
89
90 int y = 0;
91 for (int i = 0; i < 2; i++) {
92 const SkImageFilter::CropRect* cr = (i == 0) ? NULL : &cropRect;
93 paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd, NULL, cr))->unref();
94 drawClippedBitmap(canvas, paint, 0, y);
95
96 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd, NULL, cr))->unref();
97 drawClippedBitmap(canvas, paint, 110, y);
98
99 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, NULL, cr))->unref();
100 drawClippedBitmap(canvas, paint, 220, y);
101
102 y += 110;
103
104 paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess, NULL, cr))->unref();
105 drawClippedBitmap(canvas, paint, 0, y);
106
107 paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess, NULL, cr))->unref();
108 drawClippedBitmap(canvas, paint, 110, y);
109
110 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess, NULL, cr))->unref();
111 drawClippedBitmap(canvas, paint, 220, y);
112
113 y += 110;
114 }
115 }
116
117 private:
118 typedef GM INHERITED;
119 SkBitmap fBitmap;
120 bool fInitialized;
121 };
122
123 //////////////////////////////////////////////////////////////////////////////
124
MyFactory(void *)125 static GM* MyFactory(void*) { return new ImageLightingGM; }
126 static GMRegistry reg(MyFactory);
127
128 }
129