OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
11 #include "SkEmptyShader.h" | 11 #include "SkEmptyShader.h" |
12 #include "SkErrorInternals.h" | 12 #include "SkErrorInternals.h" |
13 #include "SkLightingShader.h" | 13 #include "SkLightingShader.h" |
14 #include "SkMathPriv.h" | 14 #include "SkMathPriv.h" |
15 #include "SkReadBuffer.h" | 15 #include "SkReadBuffer.h" |
16 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
17 | 17 |
18 ////////////////////////////////////////////////////////////////////////////////
/////////// | 18 //////////////////////////////////////////////////////////////////////////// |
19 | 19 |
20 /* | 20 /* |
21 SkLightingShader TODOs: | 21 SkLightingShader TODOs: |
22 support other than clamp mode | 22 support other than clamp mode |
23 allow 'diffuse' & 'normal' to be of different dimensions? | 23 allow 'diffuse' & 'normal' to be of different dimensions? |
24 support different light types | 24 support different light types |
25 support multiple lights | 25 support multiple lights |
26 enforce normal map is 4 channel | 26 enforce normal map is 4 channel |
27 use SkImages instead if SkBitmaps | 27 use SkImages instead if SkBitmaps |
28 vec3 for ambient and light-color | 28 vec3 for ambient and light-color |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 86 |
87 private: | 87 private: |
88 SkBitmapProcState* fDiffuseState; | 88 SkBitmapProcState* fDiffuseState; |
89 SkBitmapProcState* fNormalState; | 89 SkBitmapProcState* fNormalState; |
90 uint32_t fFlags; | 90 uint32_t fFlags; |
91 | 91 |
92 typedef SkShader::Context INHERITED; | 92 typedef SkShader::Context INHERITED; |
93 }; | 93 }; |
94 | 94 |
95 SK_TO_STRING_OVERRIDE() | 95 SK_TO_STRING_OVERRIDE() |
96 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(LightingShader) | 96 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) |
97 | 97 |
98 protected: | 98 protected: |
99 void flatten(SkWriteBuffer&) const override; | 99 void flatten(SkWriteBuffer&) const override; |
100 Context* onCreateContext(const ContextRec&, void*) const override; | 100 Context* onCreateContext(const ContextRec&, void*) const override; |
101 | 101 |
102 private: | 102 private: |
103 SkBitmap fDiffuseMap; | 103 SkBitmap fDiffuseMap; |
104 SkBitmap fNormalMap; | 104 SkBitmap fNormalMap; |
105 SkLightingShader::Light fLight; | 105 SkLightingShader::Light fLight; |
106 SkColor fAmbientColor; // linear (unpremul) color | 106 SkColor fAmbientColor; // linear (unpremul) color |
107 | 107 |
| 108 friend class SkLightingShader; |
| 109 |
108 typedef SkShader INHERITED; | 110 typedef SkShader INHERITED; |
109 }; | 111 }; |
110 | 112 |
111 //////////////////////////////////////////////////////////////////////////// | 113 //////////////////////////////////////////////////////////////////////////// |
112 | 114 |
113 #if SK_SUPPORT_GPU | 115 #if SK_SUPPORT_GPU |
114 | 116 |
115 #include "GrCoordTransform.h" | 117 #include "GrCoordTransform.h" |
116 #include "GrFragmentProcessor.h" | 118 #include "GrFragmentProcessor.h" |
117 #include "GrTextureAccess.h" | 119 #include "GrTextureAccess.h" |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 } | 442 } |
441 | 443 |
442 diffMProc(*fDiffuseState, tmpColor, n, x, y); | 444 diffMProc(*fDiffuseState, tmpColor, n, x, y); |
443 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); | 445 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); |
444 | 446 |
445 normalMProc(*fNormalState, tmpNormal, n, x, y); | 447 normalMProc(*fNormalState, tmpNormal, n, x, y); |
446 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); | 448 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); |
447 | 449 |
448 for (int i = 0; i < n; ++i) { | 450 for (int i = 0; i < n; ++i) { |
449 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | 451 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul |
450 norm.set(SkIntToScalar(SkColorGetR(tmpNormal2[i]))-127.0f, | 452 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, |
451 SkIntToScalar(SkColorGetG(tmpNormal2[i]))-127.0f, | 453 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, |
452 SkIntToScalar(SkColorGetB(tmpNormal2[i]))-127.0f); | 454 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); |
453 norm.normalize(); | 455 norm.normalize(); |
454 | 456 |
455 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); | 457 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); |
456 NdotL = norm.dot(lightShader.fLight.fDirection); | 458 NdotL = norm.dot(lightShader.fLight.fDirection); |
457 | 459 |
458 // This is all done in linear unpremul color space | 460 // This is all done in linear unpremul color space |
459 r = light(SkColorGetR(lightShader.fLight.fColor), SkColorGetR(diffCo
lor), NdotL, | 461 r = light(SkColorGetR(lightShader.fLight.fColor), SkColorGetR(diffCo
lor), NdotL, |
460 SkColorGetR(lightShader.fAmbientColor)); | 462 SkColorGetR(lightShader.fAmbientColor)); |
461 g = light(SkColorGetG(lightShader.fLight.fColor), SkColorGetG(diffCo
lor), NdotL, | 463 g = light(SkColorGetG(lightShader.fLight.fColor), SkColorGetG(diffCo
lor), NdotL, |
462 SkColorGetG(lightShader.fAmbientColor)); | 464 SkColorGetG(lightShader.fAmbientColor)); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 normal.isNull() || bitmap_is_too_big(normal) || | 577 normal.isNull() || bitmap_is_too_big(normal) || |
576 diffuse.width() != normal.width() || | 578 diffuse.width() != normal.width() || |
577 diffuse.height() != normal.height()) { | 579 diffuse.height() != normal.height()) { |
578 return nullptr; | 580 return nullptr; |
579 } | 581 } |
580 | 582 |
581 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, lo
calMatrix)); | 583 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, lo
calMatrix)); |
582 } | 584 } |
583 | 585 |
584 /////////////////////////////////////////////////////////////////////////////// | 586 /////////////////////////////////////////////////////////////////////////////// |
| 587 |
| 588 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
| 589 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
| 590 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 591 |
| 592 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |