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 "include/gpu/gl/GrGLInterface.h" 14 #include "src/gpu/gl/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 */ asIntsGrGLIRect29 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 } asIntsGrGLIRect38 int* asInts() { return &fLeft; } 39 pushToGLViewportGrGLIRect40 void pushToGLViewport(const GrGLInterface* gl) const { 41 GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight)); 42 } 43 pushToGLScissorGrGLIRect44 void pushToGLScissor(const GrGLInterface* gl) const { 45 GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight)); 46 } 47 setFromGLViewportGrGLIRect48 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 // The GL's viewport will always be the full size of the 57 // current render target so we just pass in the rtHeight 58 // here. setRelativeToGrGLIRect59 void setRelativeTo(int rtHeight, const SkIRect& devRect, GrSurfaceOrigin org) { 60 this->setRelativeTo(rtHeight, devRect.x(), devRect.y(), devRect.width(), devRect.height(), 61 org); 62 } 63 setRelativeToGrGLIRect64 void setRelativeTo(int fullHeight, 65 int leftOffset, 66 int topOffset, 67 int width, 68 int height, 69 GrSurfaceOrigin origin) { 70 fLeft = leftOffset; 71 fWidth = width; 72 if (kBottomLeft_GrSurfaceOrigin == origin) { 73 fBottom = fullHeight - topOffset - height; 74 } else { 75 fBottom = topOffset; 76 } 77 fHeight = height; 78 79 SkASSERT(fWidth >= 0); 80 SkASSERT(fHeight >= 0); 81 } 82 containsGrGLIRect83 bool contains(int width, int height) const { 84 return fLeft <= 0 && 85 fBottom <= 0 && 86 fLeft + fWidth >= width && 87 fBottom + fHeight >= height; 88 } 89 invalidateGrGLIRect90 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;} isInvalidGrGLIRect91 bool isInvalid() const { return fLeft == -1 && fWidth == -1 && fBottom == -1 92 && fHeight == -1; } 93 94 bool operator ==(const GrGLIRect& glRect) const { 95 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect)); 96 } 97 98 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);} 99 }; 100 101 #endif 102