GrRectanizer_pow2.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2014 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 GrRectanizer_pow2_DEFINED
  8. #define GrRectanizer_pow2_DEFINED
  9. #include "include/private/SkMalloc.h"
  10. #include "src/core/SkIPoint16.h"
  11. #include "src/core/SkMathPriv.h"
  12. #include "src/gpu/GrRectanizer.h"
  13. // This Rectanizer quantizes the incoming rects to powers of 2. Each power
  14. // of two can have, at most, one active row/shelf. Once a row/shelf for
  15. // a particular power of two gets full its fRows entry is recycled to point
  16. // to a new row.
  17. // The skyline algorithm almost always provides a better packing.
  18. class GrRectanizerPow2 : public GrRectanizer {
  19. public:
  20. GrRectanizerPow2(int w, int h) : INHERITED(w, h) {
  21. this->reset();
  22. }
  23. ~GrRectanizerPow2() override {}
  24. void reset() override {
  25. fNextStripY = 0;
  26. fAreaSoFar = 0;
  27. sk_bzero(fRows, sizeof(fRows));
  28. }
  29. bool addRect(int w, int h, SkIPoint16* loc) override;
  30. float percentFull() const override {
  31. return fAreaSoFar / ((float)this->width() * this->height());
  32. }
  33. private:
  34. static const int kMIN_HEIGHT_POW2 = 2;
  35. static const int kMaxExponent = 16;
  36. struct Row {
  37. SkIPoint16 fLoc;
  38. // fRowHeight is actually known by this struct's position in fRows
  39. // but it is used to signal if there exists an open row of this height
  40. int fRowHeight;
  41. bool canAddWidth(int width, int containerWidth) const {
  42. return fLoc.fX + width <= containerWidth;
  43. }
  44. };
  45. Row fRows[kMaxExponent]; // 0-th entry will be unused
  46. int fNextStripY;
  47. int32_t fAreaSoFar;
  48. static int HeightToRowIndex(int height) {
  49. SkASSERT(height >= kMIN_HEIGHT_POW2);
  50. int index = 32 - SkCLZ(height - 1);
  51. SkASSERT(index < kMaxExponent);
  52. return index;
  53. }
  54. bool canAddStrip(int height) const {
  55. return fNextStripY + height <= this->height();
  56. }
  57. void initRow(Row* row, int rowHeight) {
  58. row->fLoc.set(0, fNextStripY);
  59. row->fRowHeight = rowHeight;
  60. fNextStripY += rowHeight;
  61. }
  62. typedef GrRectanizer INHERITED;
  63. };
  64. #endif