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