blob: 24b5b2501147018c6623d3bc29cecabc7fe1637c [file] [log] [blame]
twiz@google.com0f31ca72011-03-18 17:38:11 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
twiz@google.com0f31ca72011-03-18 17:38:11 +00006 */
7
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00008
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Chris Daltond6cda8d2019-09-05 02:30:04 -060010#ifndef GrNativeRect_DEFINED
11#define GrNativeRect_DEFINED
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000012
Chris Dalton76500e52019-09-05 02:13:05 -060013#include "include/core/SkRect.h"
14#include "include/gpu/GrTypes.h"
twiz@google.com0f31ca72011-03-18 17:38:11 +000015
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000016/**
Chris Daltond6cda8d2019-09-05 02:30:04 -060017 * Helper struct for dealing with bottom-up surface origins (bottom-up instead of top-down).
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000018 */
Chris Daltond6cda8d2019-09-05 02:30:04 -060019struct GrNativeRect {
Chris Dalton76500e52019-09-05 02:13:05 -060020 int fX;
21 int fY;
22 int fWidth;
23 int fHeight;
24
Chris Daltond6cda8d2019-09-05 02:30:04 -060025 static GrNativeRect MakeRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) {
26 GrNativeRect nativeRect;
27 nativeRect.setRelativeTo(org, rtHeight, devRect);
28 return nativeRect;
Chris Dalton76500e52019-09-05 02:13:05 -060029 }
30
Chris Daltond6cda8d2019-09-05 02:30:04 -060031 static GrNativeRect MakeRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset,
32 int topOffset, int width, int height) {
33 GrNativeRect nativeRect;
34 nativeRect.setRelativeTo(origin, surfaceHeight, leftOffset, topOffset, width, height);
35 return nativeRect;
Chris Dalton76500e52019-09-05 02:13:05 -060036 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000037
csmartdalton28341fa2016-08-17 10:00:21 -070038 /**
39 * cast-safe way to treat the rect as an array of (4) ints.
40 */
41 const int* asInts() const {
Chris Dalton76500e52019-09-05 02:13:05 -060042 return &fX;
csmartdalton28341fa2016-08-17 10:00:21 -070043
Brian Salomonb0047b52019-12-05 19:52:25 +000044 GR_STATIC_ASSERT(0 == offsetof(GrNativeRect, fX));
45 GR_STATIC_ASSERT(4 == offsetof(GrNativeRect, fY));
46 GR_STATIC_ASSERT(8 == offsetof(GrNativeRect, fWidth));
47 GR_STATIC_ASSERT(12 == offsetof(GrNativeRect, fHeight));
48 GR_STATIC_ASSERT(16 == sizeof(GrNativeRect)); // For an array of GrNativeRect.
csmartdalton28341fa2016-08-17 10:00:21 -070049 }
Chris Dalton76500e52019-09-05 02:13:05 -060050 int* asInts() { return &fX; }
csmartdalton28341fa2016-08-17 10:00:21 -070051
Chris Dalton76500e52019-09-05 02:13:05 -060052 SkIRect asSkIRect() const { return SkIRect::MakeXYWH(fX, fY, fWidth, fHeight); }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000053
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000054 // sometimes we have a SkIRect from the client that we
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000055 // want to simultaneously make relative to GL's viewport
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000056 // and (optionally) convert from top-down to bottom-up.
Greg Danielacd66b42019-05-22 16:29:12 -040057 // The GL's viewport will always be the full size of the
58 // current render target so we just pass in the rtHeight
59 // here.
Chris Dalton76500e52019-09-05 02:13:05 -060060 void setRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) {
61 this->setRelativeTo(org, rtHeight, devRect.x(), devRect.y(), devRect.width(),
62 devRect.height());
csmartdalton28341fa2016-08-17 10:00:21 -070063 }
64
Chris Dalton76500e52019-09-05 02:13:05 -060065 void setRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset, int topOffset,
66 int width, int height) {
67 fX = leftOffset;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000068 fWidth = width;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000069 if (kBottomLeft_GrSurfaceOrigin == origin) {
Chris Dalton76500e52019-09-05 02:13:05 -060070 fY = surfaceHeight - topOffset - height;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000071 } else {
Chris Dalton76500e52019-09-05 02:13:05 -060072 fY = topOffset;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000073 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000074 fHeight = height;
75
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000076 SkASSERT(fWidth >= 0);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000077 SkASSERT(fHeight >= 0);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000078 }
79
Greg Danielacd66b42019-05-22 16:29:12 -040080 bool contains(int width, int height) const {
Chris Dalton76500e52019-09-05 02:13:05 -060081 return fX <= 0 &&
82 fY <= 0 &&
83 fX + fWidth >= width &&
84 fY + fHeight >= height;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000085 }
86
Chris Dalton76500e52019-09-05 02:13:05 -060087 void invalidate() {fX = fWidth = fY = fHeight = -1;}
88 bool isInvalid() const { return fX == -1 && fWidth == -1 && fY == -1
Adrienne Walker3a69c742018-05-21 11:05:48 -070089 && fHeight == -1; }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000090
Chris Daltond6cda8d2019-09-05 02:30:04 -060091 bool operator ==(const GrNativeRect& that) const {
92 return 0 == memcmp(this, &that, sizeof(GrNativeRect));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000093 }
94
Chris Daltond6cda8d2019-09-05 02:30:04 -060095 bool operator !=(const GrNativeRect& that) const {return !(*this == that);}
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000096};
97
reed@google.comaff86f32011-02-18 21:07:35 +000098#endif