| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkError.h" | 8 #include "SkNormalMapSource.h" |
| 9 #include "SkErrorInternals.h" | 9 |
| 10 #include "SkLightingShader.h" | 10 #include "SkLightingShader.h" |
| 11 #include "SkMatrix.h" | 11 #include "SkMatrix.h" |
| 12 #include "SkNormalSource.h" | 12 #include "SkNormalSource.h" |
| 13 #include "SkPM4f.h" | 13 #include "SkPM4f.h" |
| 14 #include "SkReadBuffer.h" | 14 #include "SkReadBuffer.h" |
| 15 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
| 16 | 16 |
| 17 // Genretating vtable | |
| 18 SkNormalSource::~SkNormalSource() {} | |
| 19 | |
| 20 /////////////////////////////////////////////////////////////////////////////// | |
| 21 | |
| 22 class NormalMapSourceImpl : public SkNormalSource { | |
| 23 public: | |
| 24 NormalMapSourceImpl(sk_sp<SkShader> mapShader, const SkMatrix& invCTM) | |
| 25 : fMapShader(std::move(mapShader)) | |
| 26 , fInvCTM(invCTM) {} | |
| 27 | |
| 28 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 29 sk_sp<GrFragmentProcessor> asFragmentProcessor(const SkShader::AsFPArgs&) co
nst override; | |
| 30 #endif | |
| 31 | |
| 32 SkNormalSource::Provider* asProvider(const SkShader::ContextRec& rec, | |
| 33 void* storage) const ov
erride; | |
| 34 | |
| 35 size_t providerSize(const SkShader::ContextRec& rec) const override; | |
| 36 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl) | |
| 37 | |
| 38 protected: | |
| 39 void flatten(SkWriteBuffer& buf) const override; | |
| 40 | |
| 41 bool computeNormTotalInverse(const SkShader::ContextRec& rec, SkMatrix* norm
TotalInverse) const; | |
| 42 | |
| 43 private: | |
| 44 class Provider : public SkNormalSource::Provider { | |
| 45 public: | |
| 46 Provider(const NormalMapSourceImpl& source, SkShader::Context* mapContex
t, | |
| 47 SkPaint* overridePaint); | |
| 48 | |
| 49 virtual ~Provider() override; | |
| 50 | |
| 51 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over
ride; | |
| 52 | |
| 53 private: | |
| 54 const NormalMapSourceImpl& fSource; | |
| 55 SkShader::Context* fMapContext; | |
| 56 | |
| 57 SkPaint* fOverridePaint; | |
| 58 | |
| 59 typedef SkNormalSource::Provider INHERITED; | |
| 60 }; | |
| 61 | |
| 62 sk_sp<SkShader> fMapShader; | |
| 63 SkMatrix fInvCTM; // Inverse of the canvas total matrix, used for rot
ating normals. | |
| 64 | |
| 65 friend class SkNormalSource; | |
| 66 | |
| 67 typedef SkNormalSource INHERITED; | |
| 68 }; | |
| 69 | |
| 70 //////////////////////////////////////////////////////////////////////////// | |
| 71 | |
| 72 #if SK_SUPPORT_GPU | |
| 73 | |
| 74 #include "GrCoordTransform.h" | 18 #include "GrCoordTransform.h" |
| 75 #include "GrInvariantOutput.h" | 19 #include "GrInvariantOutput.h" |
| 76 #include "GrTextureParams.h" | 20 #include "GrTextureParams.h" |
| 77 #include "glsl/GrGLSLFragmentProcessor.h" | 21 #include "glsl/GrGLSLFragmentProcessor.h" |
| 78 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 22 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 79 #include "SkGr.h" | 23 #include "SkGr.h" |
| 80 | 24 |
| 81 class NormalMapFP : public GrFragmentProcessor { | 25 class NormalMapFP : public GrFragmentProcessor { |
| 82 public: | 26 public: |
| 83 NormalMapFP(sk_sp<GrFragmentProcessor> mapFP, const SkMatrix& invCTM) | 27 NormalMapFP(sk_sp<GrFragmentProcessor> mapFP, const SkMatrix& invCTM) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalMapFP; } | 110 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalMapFP; } |
| 167 | 111 |
| 168 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 112 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
| 169 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); | 113 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); |
| 170 return fInvCTM == normalMapFP.fInvCTM; | 114 return fInvCTM == normalMapFP.fInvCTM; |
| 171 } | 115 } |
| 172 | 116 |
| 173 SkMatrix fInvCTM; | 117 SkMatrix fInvCTM; |
| 174 }; | 118 }; |
| 175 | 119 |
| 176 sk_sp<GrFragmentProcessor> NormalMapSourceImpl::asFragmentProcessor( | 120 sk_sp<GrFragmentProcessor> SkNormalMapSourceImpl::asFragmentProcessor( |
| 177 const SkShader::AsFPArgs& args) const { | 121 const SkShader::AsFPArgs& args) const { |
| 178 sk_sp<GrFragmentProcessor> mapFP = fMapShader->asFragmentProcessor(args); | 122 sk_sp<GrFragmentProcessor> mapFP = fMapShader->asFragmentProcessor(args); |
| 179 if (!mapFP) { | 123 if (!mapFP) { |
| 180 return nullptr; | 124 return nullptr; |
| 181 } | 125 } |
| 182 | 126 |
| 183 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); | 127 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); |
| 184 } | 128 } |
| 185 | 129 |
| 186 #endif // SK_SUPPORT_GPU | 130 #endif // SK_SUPPORT_GPU |
| 187 | 131 |
| 188 //////////////////////////////////////////////////////////////////////////// | 132 //////////////////////////////////////////////////////////////////////////// |
| 189 | 133 |
| 190 NormalMapSourceImpl::Provider::Provider(const NormalMapSourceImpl& source, | 134 SkNormalMapSourceImpl::Provider::Provider(const SkNormalMapSourceImpl& source, |
| 191 SkShader::Context* mapContext, | 135 SkShader::Context* mapContext, |
| 192 SkPaint* overridePaint) | 136 SkPaint* overridePaint) |
| 193 : fSource(source) | 137 : fSource(source) |
| 194 , fMapContext(mapContext) | 138 , fMapContext(mapContext) |
| 195 , fOverridePaint(overridePaint) {} | 139 , fOverridePaint(overridePaint) {} |
| 196 | 140 |
| 197 NormalMapSourceImpl::Provider::~Provider() { | 141 SkNormalMapSourceImpl::Provider::~Provider() { |
| 198 fMapContext->~Context(); | 142 fMapContext->~Context(); |
| 199 fOverridePaint->~SkPaint(); | 143 fOverridePaint->~SkPaint(); |
| 200 } | 144 } |
| 201 | 145 |
| 202 SkNormalSource::Provider* NormalMapSourceImpl::asProvider( | 146 SkNormalSource::Provider* SkNormalMapSourceImpl::asProvider( |
| 203 const SkShader::ContextRec &rec, void *storage) const { | 147 const SkShader::ContextRec &rec, void *storage) const { |
| 204 SkMatrix normTotalInv; | 148 SkMatrix normTotalInv; |
| 205 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { | 149 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { |
| 206 return nullptr; | 150 return nullptr; |
| 207 } | 151 } |
| 208 | 152 |
| 209 // Overriding paint's alpha because we need the normal map's RGB channels to
be unpremul'd | 153 // Overriding paint's alpha because we need the normal map's RGB channels to
be unpremul'd |
| 210 void* paintStorage = (char*)storage + sizeof(Provider); | 154 void* paintStorage = (char*)storage + sizeof(Provider); |
| 211 SkPaint* overridePaint = new (paintStorage) SkPaint(*(rec.fPaint)); | 155 SkPaint* overridePaint = new (paintStorage) SkPaint(*(rec.fPaint)); |
| 212 overridePaint->setAlpha(0xFF); | 156 overridePaint->setAlpha(0xFF); |
| 213 SkShader::ContextRec overrideRec(*overridePaint, *(rec.fMatrix), rec.fLocalM
atrix, | 157 SkShader::ContextRec overrideRec(*overridePaint, *(rec.fMatrix), rec.fLocalM
atrix, |
| 214 rec.fPreferredDstType); | 158 rec.fPreferredDstType); |
| 215 | 159 |
| 216 void* mapContextStorage = (char*) paintStorage + sizeof(SkPaint); | 160 void* mapContextStorage = (char*) paintStorage + sizeof(SkPaint); |
| 217 SkShader::Context* context = fMapShader->createContext(overrideRec, mapConte
xtStorage); | 161 SkShader::Context* context = fMapShader->createContext(overrideRec, mapConte
xtStorage); |
| 218 if (!context) { | 162 if (!context) { |
| 219 return nullptr; | 163 return nullptr; |
| 220 } | 164 } |
| 221 | 165 |
| 222 return new (storage) Provider(*this, context, overridePaint); | 166 return new (storage) Provider(*this, context, overridePaint); |
| 223 } | 167 } |
| 224 | 168 |
| 225 size_t NormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) const
{ | 169 size_t SkNormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) cons
t { |
| 226 return sizeof(Provider) + sizeof(SkPaint) + fMapShader->contextSize(rec); | 170 return sizeof(Provider) + sizeof(SkPaint) + fMapShader->contextSize(rec); |
| 227 } | 171 } |
| 228 | 172 |
| 229 bool NormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec& re
c, | 173 bool SkNormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec&
rec, |
| 230 SkMatrix* normTotalInverse) co
nst { | 174 SkMatrix* normTotalInverse) co
nst { |
| 231 SkMatrix total; | 175 SkMatrix total; |
| 232 total.setConcat(*rec.fMatrix, fMapShader->getLocalMatrix()); | 176 total.setConcat(*rec.fMatrix, fMapShader->getLocalMatrix()); |
| 233 | 177 |
| 234 const SkMatrix* m = &total; | 178 const SkMatrix* m = &total; |
| 235 if (rec.fLocalMatrix) { | 179 if (rec.fLocalMatrix) { |
| 236 total.setConcat(*m, *rec.fLocalMatrix); | 180 total.setConcat(*m, *rec.fLocalMatrix); |
| 237 m = &total; | 181 m = &total; |
| 238 } | 182 } |
| 239 return m->invert(normTotalInverse); | 183 return m->invert(normTotalInverse); |
| 240 } | 184 } |
| 241 | 185 |
| 242 #define BUFFER_MAX 16 | 186 #define BUFFER_MAX 16 |
| 243 void NormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[]
, | 187 void SkNormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output
[], |
| 244 int count) const { | 188 int count) const { |
| 245 SkPMColor tmpNormalColors[BUFFER_MAX]; | 189 SkPMColor tmpNormalColors[BUFFER_MAX]; |
| 246 | 190 |
| 247 do { | 191 do { |
| 248 int n = SkTMin(count, BUFFER_MAX); | 192 int n = SkTMin(count, BUFFER_MAX); |
| 249 | 193 |
| 250 fMapContext->shadeSpan(x, y, tmpNormalColors, n); | 194 fMapContext->shadeSpan(x, y, tmpNormalColors, n); |
| 251 | 195 |
| 252 for (int i = 0; i < n; i++) { | 196 for (int i = 0; i < n; i++) { |
| 253 SkPoint3 tempNorm; | 197 SkPoint3 tempNorm; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 284 } | 228 } |
| 285 | 229 |
| 286 output += n; | 230 output += n; |
| 287 x += n; | 231 x += n; |
| 288 count -= n; | 232 count -= n; |
| 289 } while (count > 0); | 233 } while (count > 0); |
| 290 } | 234 } |
| 291 | 235 |
| 292 //////////////////////////////////////////////////////////////////////////////// | 236 //////////////////////////////////////////////////////////////////////////////// |
| 293 | 237 |
| 294 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { | 238 sk_sp<SkFlattenable> SkNormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { |
| 295 | 239 |
| 296 sk_sp<SkShader> mapShader = buf.readFlattenable<SkShader>(); | 240 sk_sp<SkShader> mapShader = buf.readFlattenable<SkShader>(); |
| 297 | 241 |
| 298 SkMatrix invCTM; | 242 SkMatrix invCTM; |
| 299 buf.readMatrix(&invCTM); | 243 buf.readMatrix(&invCTM); |
| 300 | 244 |
| 301 return sk_make_sp<NormalMapSourceImpl>(std::move(mapShader), invCTM); | 245 return sk_make_sp<SkNormalMapSourceImpl>(std::move(mapShader), invCTM); |
| 302 } | 246 } |
| 303 | 247 |
| 304 void NormalMapSourceImpl::flatten(SkWriteBuffer& buf) const { | 248 void SkNormalMapSourceImpl::flatten(SkWriteBuffer& buf) const { |
| 305 this->INHERITED::flatten(buf); | 249 this->INHERITED::flatten(buf); |
| 306 | 250 |
| 307 buf.writeFlattenable(fMapShader.get()); | 251 buf.writeFlattenable(fMapShader.get()); |
| 308 buf.writeMatrix(fInvCTM); | 252 buf.writeMatrix(fInvCTM); |
| 309 } | 253 } |
| 310 | 254 |
| 311 //////////////////////////////////////////////////////////////////////////// | 255 //////////////////////////////////////////////////////////////////////////// |
| 312 | 256 |
| 313 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con
st SkMatrix& ctm) { | 257 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con
st SkMatrix& ctm) { |
| 314 SkMatrix invCTM; | 258 SkMatrix invCTM; |
| 315 | 259 |
| 316 if (!ctm.invert(&invCTM) || !map) { | 260 if (!ctm.invert(&invCTM) || !map) { |
| 317 return nullptr; | 261 return nullptr; |
| 318 } | 262 } |
| 319 | 263 |
| 320 return sk_make_sp<NormalMapSourceImpl>(std::move(map), invCTM); | 264 return sk_make_sp<SkNormalMapSourceImpl>(std::move(map), invCTM); |
| 321 } | 265 } |
| 322 | |
| 323 /////////////////////////////////////////////////////////////////////////////// | |
| 324 | |
| 325 class SK_API NormalFlatSourceImpl : public SkNormalSource { | |
| 326 public: | |
| 327 NormalFlatSourceImpl(){} | |
| 328 | |
| 329 #if SK_SUPPORT_GPU | |
| 330 sk_sp<GrFragmentProcessor> asFragmentProcessor(const SkShader::AsFPArgs&) co
nst override; | |
| 331 #endif | |
| 332 | |
| 333 SkNormalSource::Provider* asProvider(const SkShader::ContextRec& rec, | |
| 334 void* storage) const override; | |
| 335 size_t providerSize(const SkShader::ContextRec& rec) const override; | |
| 336 | |
| 337 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalFlatSourceImpl) | |
| 338 | |
| 339 protected: | |
| 340 void flatten(SkWriteBuffer& buf) const override; | |
| 341 | |
| 342 private: | |
| 343 class Provider : public SkNormalSource::Provider { | |
| 344 public: | |
| 345 Provider(); | |
| 346 | |
| 347 virtual ~Provider(); | |
| 348 | |
| 349 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over
ride; | |
| 350 | |
| 351 private: | |
| 352 typedef SkNormalSource::Provider INHERITED; | |
| 353 }; | |
| 354 | |
| 355 friend class SkNormalSource; | |
| 356 | |
| 357 typedef SkNormalSource INHERITED; | |
| 358 }; | |
| 359 | |
| 360 //////////////////////////////////////////////////////////////////////////// | |
| 361 | |
| 362 #if SK_SUPPORT_GPU | |
| 363 | |
| 364 class NormalFlatFP : public GrFragmentProcessor { | |
| 365 public: | |
| 366 NormalFlatFP() { | |
| 367 this->initClassID<NormalFlatFP>(); | |
| 368 } | |
| 369 | |
| 370 class GLSLNormalFlatFP : public GrGLSLFragmentProcessor { | |
| 371 public: | |
| 372 GLSLNormalFlatFP() {} | |
| 373 | |
| 374 void emitCode(EmitArgs& args) override { | |
| 375 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | |
| 376 | |
| 377 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor
); | |
| 378 } | |
| 379 | |
| 380 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | |
| 381 GrProcessorKeyBuilder* b) { | |
| 382 b->add32(0x0); | |
| 383 } | |
| 384 | |
| 385 protected: | |
| 386 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override {} | |
| 387 }; | |
| 388 | |
| 389 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { | |
| 390 GLSLNormalFlatFP::GenKey(*this, caps, b); | |
| 391 } | |
| 392 | |
| 393 const char* name() const override { return "NormalFlatFP"; } | |
| 394 | |
| 395 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
| 396 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); | |
| 397 } | |
| 398 | |
| 399 private: | |
| 400 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalFlatFP; } | |
| 401 | |
| 402 bool onIsEqual(const GrFragmentProcessor& proc) const override { | |
| 403 return true; | |
| 404 } | |
| 405 }; | |
| 406 | |
| 407 sk_sp<GrFragmentProcessor> NormalFlatSourceImpl::asFragmentProcessor( | |
| 408 const SkShader::AsFPArgs&) const { | |
| 409 | |
| 410 return sk_make_sp<NormalFlatFP>(); | |
| 411 } | |
| 412 | |
| 413 #endif // SK_SUPPORT_GPU | |
| 414 | |
| 415 //////////////////////////////////////////////////////////////////////////// | |
| 416 | |
| 417 NormalFlatSourceImpl::Provider::Provider() {} | |
| 418 | |
| 419 NormalFlatSourceImpl::Provider::~Provider() {} | |
| 420 | |
| 421 SkNormalSource::Provider* NormalFlatSourceImpl::asProvider(const SkShader::Conte
xtRec &rec, | |
| 422 void *storage) const
{ | |
| 423 return new (storage) Provider(); | |
| 424 } | |
| 425 | |
| 426 size_t NormalFlatSourceImpl::providerSize(const SkShader::ContextRec&) const { | |
| 427 return sizeof(Provider); | |
| 428 } | |
| 429 | |
| 430 void NormalFlatSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[
], | |
| 431 int count) const { | |
| 432 for (int i = 0; i < count; i++) { | |
| 433 output[i] = {0.0f, 0.0f, 1.0f}; | |
| 434 } | |
| 435 } | |
| 436 | |
| 437 //////////////////////////////////////////////////////////////////////////////// | |
| 438 | |
| 439 sk_sp<SkFlattenable> NormalFlatSourceImpl::CreateProc(SkReadBuffer& buf) { | |
| 440 return sk_make_sp<NormalFlatSourceImpl>(); | |
| 441 } | |
| 442 | |
| 443 void NormalFlatSourceImpl::flatten(SkWriteBuffer& buf) const { | |
| 444 this->INHERITED::flatten(buf); | |
| 445 } | |
| 446 | |
| 447 //////////////////////////////////////////////////////////////////////////// | |
| 448 | |
| 449 sk_sp<SkNormalSource> SkNormalSource::MakeFlat() { | |
| 450 return sk_make_sp<NormalFlatSourceImpl>(); | |
| 451 } | |
| 452 | |
| 453 //////////////////////////////////////////////////////////////////////////// | |
| 454 | |
| 455 //////////////////////////////////////////////////////////////////////////// | |
| 456 | |
| 457 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource) | |
| 458 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) | |
| 459 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalFlatSourceImpl) | |
| 460 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
| 461 | |
| 462 //////////////////////////////////////////////////////////////////////////// | |
| OLD | NEW |