1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18 #ifndef GrGLIRect_DEFINED 19 #define GrGLIRect_DEFINED 20 21 #include "GrGLInterface.h" 22 23 /** 24 * Helper struct for dealing with the fact that Ganesh and GL use different 25 * window coordinate systems (top-down vs bottom-up) 26 */ 27 struct GrGLIRect { 28 GrGLint fLeft; 29 GrGLint fBottom; 30 GrGLsizei fWidth; 31 GrGLsizei fHeight; 32 33 void pushToGLViewport() const { 34 GR_GL(Viewport(fLeft, fBottom, fWidth, fHeight)); 35 } 36 37 void pushToGLScissor() const { 38 GR_GL(Scissor(fLeft, fBottom, fWidth, fHeight)); 39 } 40 41 void setFromGLViewport() { 42 GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint)); 43 GR_GL_GetIntegerv(GR_GL_VIEWPORT, (GrGLint*) this); 44 } 45 46 // sometimes we have a GrIRect from the client that we 47 // want to simultaneously make relative to GL's viewport 48 // and convert from top-down to bottom-up. 49 void setRelativeTo(const GrGLIRect& glRect, 50 int leftOffset, 51 int topOffset, 52 int width, 53 int height) { 54 fLeft = glRect.fLeft + leftOffset; 55 fWidth = width; 56 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height); 57 fHeight = height; 58 59 GrAssert(fLeft >= 0); 60 GrAssert(fWidth >= 0); 61 GrAssert(fBottom >= 0); 62 GrAssert(fHeight >= 0); 63 } 64 65 bool contains(const GrGLIRect& glRect) const { 66 return fLeft <= glRect.fLeft && 67 fBottom <= glRect.fBottom && 68 fLeft + fWidth >= glRect.fLeft + glRect.fWidth && 69 fBottom + fHeight >= glRect.fBottom + glRect.fHeight; 70 } 71 72 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;} 73 74 bool operator ==(const GrGLIRect& glRect) const { 75 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect)); 76 } 77 78 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);} 79 }; 80 81 #endif 82