SkNormalSource.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef SkNormalSource_DEFINED
  8. #define SkNormalSource_DEFINED
  9. #include "include/core/SkFlattenable.h"
  10. #include "src/shaders/SkShaderBase.h"
  11. class SkMatrix;
  12. struct SkPoint3;
  13. #if SK_SUPPORT_GPU
  14. class GrFragmentProcessor;
  15. #endif
  16. /** Abstract class that generates or reads in normals for use by SkLightingShader.
  17. */
  18. class SK_API SkNormalSource : public SkFlattenable {
  19. public:
  20. virtual ~SkNormalSource() override;
  21. #if SK_SUPPORT_GPU
  22. /** Returns a fragment processor that takes no input and outputs a normal (already rotated)
  23. as its output color. To be used as a child fragment processor.
  24. */
  25. virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const = 0;
  26. #endif
  27. class Provider {
  28. public:
  29. virtual ~Provider() {}
  30. /** Called for each span of the object being drawn on the CPU. Your subclass should set
  31. the appropriate normals that correspond to the specified device coordinates.
  32. */
  33. virtual void fillScanLine(int x, int y, SkPoint3 output[], int count) const = 0;
  34. };
  35. /** Returns an instance of 'Provider' that provides normals for the CPU pipeline. The
  36. necessary data will be initialized in place at 'storage'.
  37. */
  38. virtual Provider* asProvider(const SkShaderBase::ContextRec&, SkArenaAlloc*) const = 0;
  39. /** Returns a normal source that provides normals sourced from the the normal map argument.
  40. @param map a shader that outputs the normal map
  41. @param ctm the current canvas' total matrix, used to rotate normals when necessary.
  42. nullptr will be returned if 'map' is null
  43. The normal map is currently assumed to be an 8888 image where the normal at a texel
  44. is retrieved by:
  45. N.x = R-127;
  46. N.y = G-127;
  47. N.z = B-127;
  48. N.normalize();
  49. The +Z axis is thus encoded in RGB as (127, 127, 255) while the -Z axis is
  50. (127, 127, 0).
  51. */
  52. static sk_sp<SkNormalSource> MakeFromNormalMap(sk_sp<SkShader> map, const SkMatrix& ctm);
  53. /** Returns a normal source that provides straight-up normals only <0, 0, 1>.
  54. */
  55. static sk_sp<SkNormalSource> MakeFlat();
  56. static Type GetFlattenableType() { return kSkNormalSource_Type; }
  57. Type getFlattenableType() const override { return GetFlattenableType(); }
  58. static sk_sp<SkNormalSource> Deserialize(const void* data, size_t size,
  59. const SkDeserialProcs* procs = nullptr) {
  60. return sk_sp<SkNormalSource>(static_cast<SkNormalSource*>(
  61. SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
  62. }
  63. static void RegisterFlattenables();
  64. };
  65. #endif