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