dvonbeck | bba4cfe | 2016-07-28 08:58:19 -0700 | [diff] [blame^] | 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 "SkPath.h" |
| 14 | #include "SkPoint3.h" |
| 15 | #include "SkShader.h" |
| 16 | |
| 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | // This GM exercises lighting shaders when used with bevel SkNormalSource objects. |
| 21 | class LightingShaderBevelGM : public GM { |
| 22 | public: |
| 23 | LightingShaderBevelGM() { |
| 24 | this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 25 | } |
| 26 | |
| 27 | protected: |
| 28 | SkString onShortName() override { |
| 29 | return SkString("lightingshaderbevel"); |
| 30 | } |
| 31 | |
| 32 | SkISize onISize() override { |
| 33 | return SkISize::Make(SkScalarCeilToInt(GRID_NUM_COLUMNS * GRID_CELL_WIDTH), |
| 34 | SkScalarCeilToInt(GRID_NUM_ROWS * GRID_CELL_WIDTH)); |
| 35 | } |
| 36 | |
| 37 | void onOnceBeforeDraw() override { |
| 38 | SkLights::Builder builder; |
| 39 | const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f); |
| 40 | |
| 41 | builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 42 | kLightFromUpperRight)); |
| 43 | builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); |
| 44 | fLights = builder.finish(); |
| 45 | |
| 46 | // fRect is assumed to be square throughout this file |
| 47 | fRect = SkRect::MakeIWH(kTexSize, kTexSize); |
| 48 | SkMatrix matrix; |
| 49 | SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize); |
| 50 | matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit); |
| 51 | |
| 52 | SkBitmap diffuseMap = sk_tool_utils::create_checkerboard_bitmap( |
| 53 | kTexSize, kTexSize, |
| 54 | sk_tool_utils::color_to_565(0x0), |
| 55 | sk_tool_utils::color_to_565(0xFF804020), |
| 56 | 8); |
| 57 | fDiffuse = SkMakeBitmapShader(diffuseMap, SkShader::kClamp_TileMode, |
| 58 | SkShader::kClamp_TileMode, &matrix, nullptr); |
| 59 | |
| 60 | fConvexPath.moveTo(fRect.width() / 2.0f, 0.0f); |
| 61 | fConvexPath.lineTo(0.0f, fRect.height()); |
| 62 | fConvexPath.lineTo(fRect.width(), fRect.height()); |
| 63 | fConvexPath.close(); |
| 64 | |
| 65 | // Creating concave path |
| 66 | { |
| 67 | SkScalar x = 0.0f; |
| 68 | SkScalar y = fRect.height() / 2.0f; |
| 69 | |
| 70 | const int NUM_SPIKES = 8; |
| 71 | |
| 72 | const SkScalar x0 = x; |
| 73 | const SkScalar dx = fRect.width() / (NUM_SPIKES * 2); |
| 74 | const SkScalar dy = SK_Scalar1 * 10; |
| 75 | |
| 76 | |
| 77 | fConcavePath.moveTo(x, y + dy); |
| 78 | for (int i = 0; i < NUM_SPIKES; i++) { |
| 79 | x += dx; |
| 80 | fConcavePath.lineTo(x, y - dy); |
| 81 | x += dx; |
| 82 | fConcavePath.lineTo(x, y + dy); |
| 83 | } |
| 84 | fConcavePath.lineTo(x, y + (2 * dy)); |
| 85 | fConcavePath.lineTo(x0, y + (2 * dy)); |
| 86 | fConcavePath.close(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Scales shape around origin, rotates shape around origin, then translates shape to origin |
| 91 | void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const { |
| 92 | canvas->translate(kTexSize/2.0f, kTexSize/2.0f); |
| 93 | canvas->scale(scaleX, scaleY); |
| 94 | canvas->rotate(rotate); |
| 95 | canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f); |
| 96 | } |
| 97 | |
| 98 | enum Shape { |
| 99 | kCircle_Shape, |
| 100 | kRect_Shape, |
| 101 | kRRect_Shape, |
| 102 | kConvexPath_Shape, |
| 103 | kConcavePath_Shape, |
| 104 | |
| 105 | kLast_Shape = kConcavePath_Shape |
| 106 | }; |
| 107 | void drawShape(enum Shape shape, SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY, |
| 108 | SkScalar rotate, SkNormalSource::BevelType bevelType, SkScalar bevelHeight) { |
| 109 | canvas->save(); |
| 110 | |
| 111 | this->positionCTM(canvas, scaleX, scaleY, rotate); |
| 112 | |
| 113 | SkPaint paint; |
| 114 | |
| 115 | SkScalar bevelWidth = 10.0f; |
| 116 | sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeBevel(bevelType, bevelWidth, |
| 117 | bevelHeight); |
| 118 | |
| 119 | paint.setShader(SkLightingShader::Make(fDiffuse, std::move(normalSource), fLights)); |
| 120 | paint.setAntiAlias(true); |
| 121 | switch(shape) { |
| 122 | case kCircle_Shape: |
| 123 | canvas->drawCircle(fRect.centerX(), fRect.centerY(), fRect.width()/2.0f, paint); |
| 124 | break; |
| 125 | case kRect_Shape: |
| 126 | canvas->drawRect(fRect, paint); |
| 127 | break; |
| 128 | case kRRect_Shape: |
| 129 | canvas->drawRoundRect(fRect, 5.0f, 5.0f, paint); |
| 130 | break; |
| 131 | case kConvexPath_Shape: |
| 132 | canvas->drawPath(fConvexPath, paint); |
| 133 | break; |
| 134 | case kConcavePath_Shape: |
| 135 | canvas->drawPath(fConcavePath, paint); |
| 136 | break; |
| 137 | default: |
| 138 | SkDEBUGFAIL("Invalid shape enum for drawShape"); |
| 139 | } |
| 140 | |
| 141 | canvas->restore(); |
| 142 | } |
| 143 | |
| 144 | void onDraw(SkCanvas* canvas) override { |
| 145 | SkPaint labelPaint; |
| 146 | labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-serif", |
| 147 | SkFontStyle())); |
| 148 | labelPaint.setAntiAlias(true); |
| 149 | labelPaint.setTextSize(LABEL_SIZE); |
| 150 | |
| 151 | int gridNum = 0; |
| 152 | |
| 153 | // Running through all possible parameter combinations |
| 154 | for (auto bevelType : {SkNormalSource::BevelType::kLinear, |
| 155 | SkNormalSource::BevelType::kRoundedIn, |
| 156 | SkNormalSource::BevelType::kRoundedOut}) { |
| 157 | for (SkScalar bevelHeight: {-7.0f, 7.0f}) { |
| 158 | for (int shapeInt = 0; shapeInt < NUM_SHAPES; shapeInt++) { |
| 159 | Shape shape = (Shape)shapeInt; |
| 160 | |
| 161 | // Determining position |
| 162 | SkScalar xPos = (gridNum / GRID_NUM_ROWS) * GRID_CELL_WIDTH; |
| 163 | SkScalar yPos = (gridNum % GRID_NUM_ROWS) * GRID_CELL_WIDTH; |
| 164 | |
| 165 | canvas->save(); |
| 166 | |
| 167 | canvas->translate(xPos, yPos); |
| 168 | this->drawShape(shape, canvas, 1.0f, 1.0f, 0.f, bevelType, bevelHeight); |
| 169 | // Drawing labels |
| 170 | canvas->translate(0.0f, SkIntToScalar(kTexSize)); |
| 171 | { |
| 172 | canvas->translate(0.0f, LABEL_SIZE); |
| 173 | SkString label; |
| 174 | label.append("bevelType: "); |
| 175 | switch (bevelType) { |
| 176 | case SkNormalSource::BevelType::kLinear: |
| 177 | label.append("linear"); |
| 178 | break; |
| 179 | case SkNormalSource::BevelType::kRoundedIn: |
| 180 | label.append("roundedIn"); |
| 181 | break; |
| 182 | case SkNormalSource::BevelType::kRoundedOut: |
| 183 | label.append("roundedOut"); |
| 184 | break; |
| 185 | } |
| 186 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 187 | } |
| 188 | { |
| 189 | canvas->translate(0.0f, LABEL_SIZE); |
| 190 | SkString label; |
| 191 | label.appendf("bevelHeight: %.1f", bevelHeight); |
| 192 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 193 | } |
| 194 | |
| 195 | canvas->restore(); |
| 196 | |
| 197 | gridNum++; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Testing rotation |
| 203 | for (int shapeInt = 0; shapeInt < NUM_SHAPES; shapeInt++) { |
| 204 | Shape shape = (Shape)shapeInt; |
| 205 | |
| 206 | // Determining position |
| 207 | SkScalar xPos = (gridNum / GRID_NUM_ROWS) * GRID_CELL_WIDTH; |
| 208 | SkScalar yPos = (gridNum % GRID_NUM_ROWS) * GRID_CELL_WIDTH; |
| 209 | |
| 210 | canvas->save(); |
| 211 | |
| 212 | canvas->translate(xPos, yPos); |
| 213 | this->drawShape(shape, canvas, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2, 45.0f, |
| 214 | SkNormalSource::BevelType::kLinear, 7.0f); |
| 215 | |
| 216 | // Drawing labels |
| 217 | canvas->translate(0.0f, SkIntToScalar(kTexSize)); |
| 218 | { |
| 219 | canvas->translate(0.0f, LABEL_SIZE); |
| 220 | SkString label; |
| 221 | label.appendf("bevelType: linear"); |
| 222 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 223 | } |
| 224 | { |
| 225 | canvas->translate(0.0f, LABEL_SIZE); |
| 226 | SkString label; |
| 227 | label.appendf("bevelHeight: %.1f", 7.0f); |
| 228 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 229 | } |
| 230 | { |
| 231 | canvas->translate(0.0f, LABEL_SIZE); |
| 232 | SkString label; |
| 233 | label.appendf("rotated"); |
| 234 | canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint); |
| 235 | } |
| 236 | |
| 237 | canvas->restore(); |
| 238 | |
| 239 | gridNum++; |
| 240 | } |
| 241 | |
| 242 | // Making sure NUM_COMBINATIONS_PER_SHAPE is set correctly |
| 243 | SkASSERT(gridNum == (NUM_COMBINATIONS_PER_SHAPE*NUM_SHAPES)); |
| 244 | } |
| 245 | |
| 246 | private: |
| 247 | static constexpr int kTexSize = 96; |
| 248 | static constexpr int NUM_SHAPES = kLast_Shape + 1; |
| 249 | static constexpr int NUM_COMBINATIONS_PER_SHAPE = 7; |
| 250 | static constexpr int GRID_NUM_ROWS = NUM_SHAPES; |
| 251 | static constexpr int GRID_NUM_COLUMNS = NUM_COMBINATIONS_PER_SHAPE; |
| 252 | static constexpr SkScalar LABEL_SIZE = 10.0f; |
| 253 | static constexpr int NUM_LABELS_PER_CELL = 3; |
| 254 | static constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 10.0f + NUM_LABELS_PER_CELL * LABEL_SIZE; |
| 255 | |
| 256 | sk_sp<SkShader> fDiffuse; |
| 257 | |
| 258 | SkRect fRect; |
| 259 | SkPath fConvexPath; |
| 260 | SkPath fConcavePath; |
| 261 | sk_sp<SkLights> fLights; |
| 262 | |
| 263 | typedef GM INHERITED; |
| 264 | }; |
| 265 | |
| 266 | ////////////////////////////////////////////////////////////////////////////// |
| 267 | |
| 268 | DEF_GM(return new LightingShaderBevelGM;) |
| 269 | } |