180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef GrGLIRect_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GrGLIRect_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/GrGLInterface.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "GrGLUtil.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Helper struct for dealing with the fact that Ganesh and GL use different
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * window coordinate systems (top-down vs bottom-up)
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustruct GrGLIRect {
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLint   fLeft;
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLint   fBottom;
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLsizei fWidth;
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLsizei fHeight;
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void pushToGLViewport(const GrGLInterface* gl) const {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight));
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void pushToGLScissor(const GrGLInterface* gl) const {
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight));
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setFromGLViewport(const GrGLInterface* gl) {
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this);
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
397839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    // sometimes we have a SkIRect from the client that we
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // want to simultaneously make relative to GL's viewport
41096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    // and (optionally) convert from top-down to bottom-up.
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setRelativeTo(const GrGLIRect& glRect,
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                       int leftOffset,
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                       int topOffset,
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                       int width,
46096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger                       int height,
47096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger                       GrSurfaceOrigin origin) {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fLeft = glRect.fLeft + leftOffset;
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fWidth = width;
50096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        if (kBottomLeft_GrSurfaceOrigin == origin) {
51096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger            fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
52096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        } else {
53096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger            fBottom = glRect.fBottom + topOffset;
54096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        }
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fHeight = height;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
570a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkASSERT(fLeft >= 0);
580a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkASSERT(fWidth >= 0);
590a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkASSERT(fBottom >= 0);
600a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkASSERT(fHeight >= 0);
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool contains(const GrGLIRect& glRect) const {
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return fLeft <= glRect.fLeft &&
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               fBottom <= glRect.fBottom &&
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               fLeft + fWidth >=  glRect.fLeft + glRect.fWidth &&
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               fBottom + fHeight >=  glRect.fBottom + glRect.fHeight;
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool operator ==(const GrGLIRect& glRect) const {
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
80