SkBitmapProcShader.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2011 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. #include "src/shaders/SkBitmapProcShader.h"
  8. #include "src/core/SkArenaAlloc.h"
  9. #include "src/core/SkBitmapProcState.h"
  10. #include "src/core/SkBitmapProvider.h"
  11. #include "src/core/SkXfermodePriv.h"
  12. static bool only_scale_and_translate(const SkMatrix& matrix) {
  13. unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
  14. return (matrix.getType() & ~mask) == 0;
  15. }
  16. class BitmapProcInfoContext : public SkShaderBase::Context {
  17. public:
  18. // The info has been allocated elsewhere, but we are responsible for calling its destructor.
  19. BitmapProcInfoContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
  20. SkBitmapProcInfo* info)
  21. : INHERITED(shader, rec)
  22. , fInfo(info)
  23. {
  24. fFlags = 0;
  25. if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) {
  26. fFlags |= SkShaderBase::kOpaqueAlpha_Flag;
  27. }
  28. if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) {
  29. fFlags |= SkShaderBase::kConstInY32_Flag;
  30. }
  31. }
  32. uint32_t getFlags() const override { return fFlags; }
  33. private:
  34. SkBitmapProcInfo* fInfo;
  35. uint32_t fFlags;
  36. typedef SkShaderBase::Context INHERITED;
  37. };
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////
  39. class BitmapProcShaderContext : public BitmapProcInfoContext {
  40. public:
  41. BitmapProcShaderContext(const SkShaderBase& shader, const SkShaderBase::ContextRec& rec,
  42. SkBitmapProcState* state)
  43. : INHERITED(shader, rec, state)
  44. , fState(state)
  45. {}
  46. void shadeSpan(int x, int y, SkPMColor dstC[], int count) override {
  47. const SkBitmapProcState& state = *fState;
  48. if (state.getShaderProc32()) {
  49. state.getShaderProc32()(&state, x, y, dstC, count);
  50. return;
  51. }
  52. const int BUF_MAX = 128;
  53. uint32_t buffer[BUF_MAX];
  54. SkBitmapProcState::MatrixProc mproc = state.getMatrixProc();
  55. SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32();
  56. const int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX);
  57. SkASSERT(state.fPixmap.addr());
  58. for (;;) {
  59. int n = SkTMin(count, max);
  60. SkASSERT(n > 0 && n < BUF_MAX*2);
  61. mproc(state, buffer, n, x, y);
  62. sproc(state, buffer, n, dstC);
  63. if ((count -= n) == 0) {
  64. break;
  65. }
  66. SkASSERT(count > 0);
  67. x += n;
  68. dstC += n;
  69. }
  70. }
  71. private:
  72. SkBitmapProcState* fState;
  73. typedef BitmapProcInfoContext INHERITED;
  74. };
  75. ///////////////////////////////////////////////////////////////////////////////////////////////////
  76. SkShaderBase::Context* SkBitmapProcLegacyShader::MakeContext(
  77. const SkShaderBase& shader, SkTileMode tmx, SkTileMode tmy,
  78. const SkBitmapProvider& provider, const ContextRec& rec, SkArenaAlloc* alloc)
  79. {
  80. SkMatrix totalInverse;
  81. // Do this first, so we know the matrix can be inverted.
  82. if (!shader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &totalInverse)) {
  83. return nullptr;
  84. }
  85. SkBitmapProcState* state = alloc->make<SkBitmapProcState>(provider, tmx, tmy);
  86. if (!state->setup(totalInverse, *rec.fPaint)) {
  87. return nullptr;
  88. }
  89. return alloc->make<BitmapProcShaderContext>(shader, rec, state);
  90. }