SkSpriteBlitter_ARGB32.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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 "include/core/SkColorFilter.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/private/SkColorData.h"
  10. #include "include/private/SkTemplates.h"
  11. #include "src/core/SkArenaAlloc.h"
  12. #include "src/core/SkBlitRow.h"
  13. #include "src/core/SkSpriteBlitter.h"
  14. #include "src/core/SkXfermodePriv.h"
  15. #include "src/utils/SkUTF.h"
  16. ///////////////////////////////////////////////////////////////////////////////
  17. class Sprite_D32_S32 : public SkSpriteBlitter {
  18. public:
  19. Sprite_D32_S32(const SkPixmap& src, U8CPU alpha) : INHERITED(src) {
  20. SkASSERT(src.colorType() == kN32_SkColorType);
  21. unsigned flags32 = 0;
  22. if (255 != alpha) {
  23. flags32 |= SkBlitRow::kGlobalAlpha_Flag32;
  24. }
  25. if (!src.isOpaque()) {
  26. flags32 |= SkBlitRow::kSrcPixelAlpha_Flag32;
  27. }
  28. fProc32 = SkBlitRow::Factory32(flags32);
  29. fAlpha = alpha;
  30. }
  31. void blitRect(int x, int y, int width, int height) override {
  32. SkASSERT(width > 0 && height > 0);
  33. uint32_t* SK_RESTRICT dst = fDst.writable_addr32(x, y);
  34. const uint32_t* SK_RESTRICT src = fSource.addr32(x - fLeft, y - fTop);
  35. size_t dstRB = fDst.rowBytes();
  36. size_t srcRB = fSource.rowBytes();
  37. SkBlitRow::Proc32 proc = fProc32;
  38. U8CPU alpha = fAlpha;
  39. do {
  40. proc(dst, src, width, alpha);
  41. dst = (uint32_t* SK_RESTRICT)((char*)dst + dstRB);
  42. src = (const uint32_t* SK_RESTRICT)((const char*)src + srcRB);
  43. } while (--height != 0);
  44. }
  45. private:
  46. SkBlitRow::Proc32 fProc32;
  47. U8CPU fAlpha;
  48. typedef SkSpriteBlitter INHERITED;
  49. };
  50. ///////////////////////////////////////////////////////////////////////////////
  51. class Sprite_D32_S32A_Xfer: public SkSpriteBlitter {
  52. public:
  53. Sprite_D32_S32A_Xfer(const SkPixmap& source, const SkPaint& paint) : SkSpriteBlitter(source) {
  54. fXfermode = SkXfermode::Peek(paint.getBlendMode());
  55. SkASSERT(fXfermode);
  56. }
  57. void blitRect(int x, int y, int width, int height) override {
  58. SkASSERT(width > 0 && height > 0);
  59. uint32_t* SK_RESTRICT dst = fDst.writable_addr32(x, y);
  60. const uint32_t* SK_RESTRICT src = fSource.addr32(x - fLeft, y - fTop);
  61. size_t dstRB = fDst.rowBytes();
  62. size_t srcRB = fSource.rowBytes();
  63. SkXfermode* xfermode = fXfermode;
  64. do {
  65. xfermode->xfer32(dst, src, width, nullptr);
  66. dst = (uint32_t* SK_RESTRICT)((char*)dst + dstRB);
  67. src = (const uint32_t* SK_RESTRICT)((const char*)src + srcRB);
  68. } while (--height != 0);
  69. }
  70. protected:
  71. SkXfermode* fXfermode;
  72. private:
  73. typedef SkSpriteBlitter INHERITED;
  74. };
  75. ///////////////////////////////////////////////////////////////////////////////
  76. SkSpriteBlitter* SkSpriteBlitter::ChooseL32(const SkPixmap& source, const SkPaint& paint,
  77. SkArenaAlloc* allocator) {
  78. SkASSERT(allocator != nullptr);
  79. if (paint.getColorFilter() != nullptr) {
  80. return nullptr;
  81. }
  82. if (paint.getMaskFilter() != nullptr) {
  83. return nullptr;
  84. }
  85. U8CPU alpha = paint.getAlpha();
  86. if (source.colorType() == kN32_SkColorType) {
  87. if (paint.isSrcOver()) {
  88. // this can handle alpha, but not xfermode
  89. return allocator->make<Sprite_D32_S32>(source, alpha);
  90. }
  91. if (255 == alpha) {
  92. // this can handle an xfermode, but not alpha
  93. return allocator->make<Sprite_D32_S32A_Xfer>(source, paint);
  94. }
  95. }
  96. return nullptr;
  97. }