GrGLIRect.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef GrGLIRect_DEFINED
  8. #define GrGLIRect_DEFINED
  9. #include "include/gpu/gl/GrGLInterface.h"
  10. #include "src/gpu/gl/GrGLUtil.h"
  11. /**
  12. * Helper struct for dealing with the fact that Ganesh and GL use different
  13. * window coordinate systems (top-down vs bottom-up)
  14. */
  15. struct GrGLIRect {
  16. GrGLint fLeft;
  17. GrGLint fBottom;
  18. GrGLsizei fWidth;
  19. GrGLsizei fHeight;
  20. /**
  21. * cast-safe way to treat the rect as an array of (4) ints.
  22. */
  23. const int* asInts() const {
  24. return &fLeft;
  25. GR_STATIC_ASSERT(0 == offsetof(GrGLIRect, fLeft));
  26. GR_STATIC_ASSERT(4 == offsetof(GrGLIRect, fBottom));
  27. GR_STATIC_ASSERT(8 == offsetof(GrGLIRect, fWidth));
  28. GR_STATIC_ASSERT(12 == offsetof(GrGLIRect, fHeight));
  29. GR_STATIC_ASSERT(16 == sizeof(GrGLIRect)); // For an array of GrGLIRect.
  30. }
  31. int* asInts() { return &fLeft; }
  32. void pushToGLViewport(const GrGLInterface* gl) const {
  33. GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight));
  34. }
  35. void pushToGLScissor(const GrGLInterface* gl) const {
  36. GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight));
  37. }
  38. void setFromGLViewport(const GrGLInterface* gl) {
  39. GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
  40. GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this);
  41. }
  42. // sometimes we have a SkIRect from the client that we
  43. // want to simultaneously make relative to GL's viewport
  44. // and (optionally) convert from top-down to bottom-up.
  45. // The GL's viewport will always be the full size of the
  46. // current render target so we just pass in the rtHeight
  47. // here.
  48. void setRelativeTo(int rtHeight, const SkIRect& devRect, GrSurfaceOrigin org) {
  49. this->setRelativeTo(rtHeight, devRect.x(), devRect.y(), devRect.width(), devRect.height(),
  50. org);
  51. }
  52. void setRelativeTo(int fullHeight,
  53. int leftOffset,
  54. int topOffset,
  55. int width,
  56. int height,
  57. GrSurfaceOrigin origin) {
  58. fLeft = leftOffset;
  59. fWidth = width;
  60. if (kBottomLeft_GrSurfaceOrigin == origin) {
  61. fBottom = fullHeight - topOffset - height;
  62. } else {
  63. fBottom = topOffset;
  64. }
  65. fHeight = height;
  66. SkASSERT(fWidth >= 0);
  67. SkASSERT(fHeight >= 0);
  68. }
  69. bool contains(int width, int height) const {
  70. return fLeft <= 0 &&
  71. fBottom <= 0 &&
  72. fLeft + fWidth >= width &&
  73. fBottom + fHeight >= height;
  74. }
  75. void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
  76. bool isInvalid() const { return fLeft == -1 && fWidth == -1 && fBottom == -1
  77. && fHeight == -1; }
  78. bool operator ==(const GrGLIRect& glRect) const {
  79. return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
  80. }
  81. bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
  82. };
  83. #endif