Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: gm/lightingshader2.cpp

Issue 2132113002: SkLS accepts nullptr for pointer args, handles alpha accurately, has new GM (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-diffuse-api-change
Patch Set: Overridden paint now gets destructed, clearer comments, GM now tests texture + transparent paint, a… Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/gpu/GrFragmentProcessor.h » ('j') | include/gpu/GrFragmentProcessor.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 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
10 #include "SkBitmapProcShader.h"
11 #include "SkLightingShader.h"
12 #include "SkNormalSource.h"
13 #include "SkPoint3.h"
14 #include "SkShader.h"
15
16 // Create a truncated pyramid normal map
17 static SkBitmap make_frustum_normalmap(int texSize) {
18 SkBitmap frustum;
19 frustum.allocN32Pixels(texSize, texSize);
20
21 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
22 return frustum;
23 }
24
25 namespace skiagm {
26
27 // This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using
28 // normal maps, and paint transparency.
29 class LightingShader2GM : public GM {
30 public:
31 LightingShader2GM() {
32 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
33 }
34
35 protected:
36 SkString onShortName() override {
37 return SkString("lightingshader2");
38 }
39
40 SkISize onISize() override {
41 return SkISize::Make(kGMSize, kGMSize);
42 }
43
44 void onOnceBeforeDraw() override {
45 SkLights::Builder builder;
46 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0 .473f);
47
48 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
49 kLightFromUpperRight));
50 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
51 fLights = builder.finish();
52
53 fDiffuse = sk_tool_utils::create_checkerboard_bitmap(
54 kTexSize, kTexSize,
55 sk_tool_utils::color_to_ 565(0x0),
56 sk_tool_utils::color_to_ 565(0xFF804020),
57 8);
58
59 fNormalMap = make_frustum_normalmap(kTexSize);
60 }
61
62 // Scales shape around origin, rotates shape around origin, then translates shape to GM center
63 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScala r rotate) const {
64 canvas->translate(kGMSize / 2.0f, kGMSize / 2.0f);
65 canvas->scale(scaleX, scaleY);
66 canvas->rotate(rotate);
67 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
68 }
69
70 void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scaleX, SkScalar s caleY,
71 SkScalar rotate, bool useNormalSource = true, bool useDiffuseS hader = true,
egdaniel 2016/07/14 03:22:47 not sure there is much advantage to the defaults h
dvonbeck 2016/07/14 14:30:33 Done.
72 bool useTransparentPaint = false) {
73 this->positionCTM(canvas, scaleX, scaleY, rotate);
74
75 SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height( ));
76
77 SkMatrix matrix;
78 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
79
80 const SkMatrix& ctm = canvas->getTotalMatrix();
81
82 SkPaint paint;
83 sk_sp<SkNormalSource> normalSource = nullptr;
84 sk_sp<SkShader> diffuseShader = nullptr;
85
86 if (useNormalSource) {
87 sk_sp <SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader ::kClamp_TileMode,
88 SkShader::kClamp_Til eMode, &matrix,
89 nullptr);
90 normalSource = SkNormalSource::MakeFromNormalMap(
91 std::move(normalMap),
92 ctm);
93 }
94 if (useDiffuseShader) {
95 diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::kClamp_TileMo de,
96 SkShader::kClamp_TileMode, &matri x, nullptr);
97 } else {
98 paint.setColor(0xFF00FF00);
99 }
100 if (useTransparentPaint) {
101 paint.setAlpha(0x55);
102 }
103 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::mo ve(normalSource),
104 fLights));
105
106 canvas->drawRect(r, paint);
107 }
108
109 void onDraw(SkCanvas* canvas) override {
110 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz e));
111
112 canvas->save();
113 canvas->translate(0.0f, -SkScalarSqrt(2.0f) * kTexSize);
114 this->drawRect(canvas, r, 1.0f, 1.0f, 45.f, true, true, true);
egdaniel 2016/07/14 03:22:47 Would there be value to drawing all these combinat
dvonbeck 2016/07/14 14:30:33 I don't think so, since I am varying different par
115 canvas->restore();
116
117 canvas->save();
118 canvas->translate(-SK_ScalarSqrt2 * kTexSize, -SK_ScalarSqrt2 * kTexSize );
119 this->drawRect(canvas, r, 1.2f, 1.2f, 0.f);
120 canvas->restore();
121
122 canvas->save();
123 canvas->translate(SK_ScalarSqrt2 * kTexSize, -SK_ScalarSqrt2 * kTexSize) ;
124 this->drawRect(canvas, r, 0.5f, 0.5f, 0.f);
125 canvas->restore();
126
127 canvas->save();
128 canvas->translate(-SK_ScalarSqrt2 * kTexSize, 0.0f);
129 this->drawRect(canvas, r, 1.0f, 1.0f, 0.f, true, false, true);
130 canvas->restore();
131
132 canvas->save();
133 canvas->translate(SK_ScalarSqrt2 * kTexSize, 0.0f);
134 this->drawRect(canvas, r, 1.0f, 1.0f, 0.f, false, true);
135 canvas->restore();
136 }
137
138 private:
139 static const int kTexSize = 128;
140 static const int kGMSize = 512;
141
142 SkBitmap fDiffuse;
143 SkBitmap fNormalMap;
144
145 sk_sp<SkLights> fLights;
146
147 typedef GM INHERITED;
148 };
149
150 //////////////////////////////////////////////////////////////////////////////
151
152 DEF_GM(return new LightingShader2GM;)
153 }
OLDNEW
« no previous file with comments | « no previous file | include/gpu/GrFragmentProcessor.h » ('j') | include/gpu/GrFragmentProcessor.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698