blob: 75099b2df6e54d96bf867bd9de9180bfa0d9bd0f [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 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 SkGr_DEFINED
19#define SkGr_DEFINED
20
21#include <stddef.h>
22
bsalomon@google.comd302f142011-03-03 13:54:13 +000023// Gr headers
reed@google.comac10a2d2010-12-22 21:39:39 +000024#include "GrConfig.h"
25#include "GrContext.h"
26#include "GrFontScaler.h"
27#include "GrPathIter.h"
28#include "GrClipIterator.h"
29
30// skia headers
31#include "SkBitmap.h"
32#include "SkPath.h"
33#include "SkPoint.h"
34#include "SkRegion.h"
35#include "SkShader.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000036#include "SkClipStack.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000037
38#if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG))
39// #error "inconsistent GR_DEBUG and SK_DEBUG"
40#endif
41
42#if GR_SCALAR_IS_FIXED
43 #ifdef SK_SCALAR_IS_FIXED
44 #define SK_SCALAR_IS_GR_SCALAR 1
45 #else
46 #define SK_SCALAR_IS_GR_SCALAR 0
47 #endif
48 #define SkScalarToGrScalar(x) SkScalarToFixed(x)
49
50#elif GR_SCALAR_IS_FLOAT
51
52 #ifdef SK_SCALAR_IS_FLOAT
53 #define SK_SCALAR_IS_GR_SCALAR 1
54 #else
55 #define SK_SCALAR_IS_GR_SCALAR 0
56 #endif
57 #define SkScalarToGrScalar(x) SkScalarToFloat(x)
58
bsalomon@google.com5782d712011-01-21 21:03:59 +000059#else
reed@google.comac10a2d2010-12-22 21:39:39 +000060 #error "Ganesh scalar type not defined"
61#endif
62
63////////////////////////////////////////////////////////////////////////////////
64// Sk to Gr Type conversions
65
66// Verify that SkPoint and GrPoint are compatible if using the same scalar type
67#if 0/*SK_SCALAR_IS_GR_SCALAR*/
68 GR_STATIC_ASSERT(sizeof(SkPoint) == sizeof(GrPoint));
69 GR_STATIC_ASSERT(offsetof(SkPoint,fX) == offsetof(GrPoint,fX)));
70 GR_STATIC_ASSERT(offsetof(SkPoint,fY) == offsetof(GrPoint,fY)));
71#endif
72
73GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode);
74GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==(
75 int)SkShader::kRepeat_TileMode);
bsalomon@google.com5782d712011-01-21 21:03:59 +000076GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode ==
reed@google.comac10a2d2010-12-22 21:39:39 +000077 (int)SkShader::kMirror_TileMode);
78
79#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X))
80
bsalomon@google.comffca4002011-02-22 20:34:01 +000081GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff);
82GR_STATIC_ASSERT((int)kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff);
83GR_STATIC_ASSERT((int)kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff);
84GR_STATIC_ASSERT((int)kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff);
85GR_STATIC_ASSERT((int)kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff);
86GR_STATIC_ASSERT((int)kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff);
87GR_STATIC_ASSERT((int)kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff);
88GR_STATIC_ASSERT((int)kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff);
89GR_STATIC_ASSERT((int)kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff);
90GR_STATIC_ASSERT((int)kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff);
reed@google.comac10a2d2010-12-22 21:39:39 +000091
bsalomon@google.comffca4002011-02-22 20:34:01 +000092#define sk_blend_to_grblend(X) ((GrBlendCoeff)(X))
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000094GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)kMove_PathCmd);
95GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)kLine_PathCmd);
96GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)kQuadratic_PathCmd);
97GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd);
98GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd);
99GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)kEnd_PathCmd);
reed@google.comac10a2d2010-12-22 21:39:39 +0000100
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000101#define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X))
reed@google.comac10a2d2010-12-22 21:39:39 +0000102
103///////////////////////////////////////////////////////////////////////////////
104
105#include "SkColorPriv.h"
106
reed@google.comac10a2d2010-12-22 21:39:39 +0000107class SkGr {
108public:
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 /**
110 * Convert the SkBitmap::Config to the corresponding PixelConfig, or
111 * kUnknown_PixelConfig if the conversion cannot be done.
112 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000113 static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config,
114 bool isOpaque);
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000116 static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque());
118 }
119
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 static GrColor SkColor2GrColor(SkColor c) {
121 SkPMColor pm = SkPreMultiplyColor(c);
122 unsigned r = SkGetPackedR32(pm);
123 unsigned g = SkGetPackedG32(pm);
124 unsigned b = SkGetPackedB32(pm);
125 unsigned a = SkGetPackedA32(pm);
126 return GrColorPackRGBA(r, g, b, a);
127 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000128};
129
130////////////////////////////////////////////////////////////////////////////////
131// Classes
132
133class SkGrPathIter : public GrPathIter {
134public:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000135 SkGrPathIter() { fPath = NULL; }
136 SkGrPathIter(const SkPath& path) { reset(path); }
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000137 virtual GrPathCmd next(GrPoint pts[]);
138 virtual GrPathCmd next();
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 virtual void rewind();
bsalomon@google.combf4338c2011-03-04 22:48:25 +0000140 virtual GrConvexHint convexHint() const;
bsalomon@google.com06e17952011-04-27 21:13:04 +0000141 virtual bool getConservativeBounds(GrRect* rect) const;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142
143 void reset(const SkPath& path) {
144 fPath = &path;
145 fIter.setPath(path, false);
146 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000147private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149#if !SK_SCALAR_IS_GR_SCALAR
150 SkPoint fPoints[4];
151#endif
152 SkPath::Iter fIter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000153 const SkPath* fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000154};
155
156class SkGrClipIterator : public GrClipIterator {
157public:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000158 SkGrClipIterator() { fClipStack = NULL; fCurr = NULL; }
159 SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); }
160
161 void reset(const SkClipStack& clipStack);
162
163 // overrides
164 virtual bool isDone() const { return NULL == fCurr; }
165 virtual void next() { fCurr = fIter.next(); }
166 virtual void rewind() { this->reset(*fClipStack); }
167 virtual GrClipType getType() const;
168
169 virtual GrSetOp getOp() const;
170
171 virtual void getRect(GrRect* rect) const {
scroggo7b118072011-03-23 15:04:26 +0000172 if (!fCurr->fRect) {
173 rect->setEmpty();
174 } else {
reed@google.com20efde72011-05-09 17:00:02 +0000175 *rect = *fCurr->fRect;
scroggo7b118072011-03-23 15:04:26 +0000176 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000177 }
178
179 virtual GrPathIter* getPathIter() {
180 fPathIter.reset(*fCurr->fPath);
181 return &fPathIter;
182 }
183
184 virtual GrPathFill getPathFill() const;
185
186private:
187 const SkClipStack* fClipStack;
188 SkClipStack::B2FIter fIter;
189 SkGrPathIter fPathIter;
190 // SkClipStack's auto advances on each get
191 // so we store the current pos here.
192 const SkClipStack::B2FIter::Clip* fCurr;
193};
194
195class SkGrRegionIterator : public GrClipIterator {
196public:
197 SkGrRegionIterator() {}
198 SkGrRegionIterator(const SkRegion& region) { this->reset(region); }
199
reed@google.com98539c62011-03-15 15:40:16 +0000200 void reset(const SkRegion& region) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000201 fRegion = &region;
202 fIter.reset(region);
reed@google.comac10a2d2010-12-22 21:39:39 +0000203 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000204
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 // overrides
bsalomon@google.comd302f142011-03-03 13:54:13 +0000206 virtual bool isDone() const { return fIter.done(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 virtual void next() { fIter.next(); }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000208 virtual void rewind() { this->reset(*fRegion); }
209 virtual GrClipType getType() const { return kRect_ClipType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
bsalomon@google.comd302f142011-03-03 13:54:13 +0000211 virtual GrSetOp getOp() const { return kUnion_SetOp; }
212
213 virtual void getRect(GrRect* rect) const {
214 const SkIRect& r = fIter.rect();
215 rect->fLeft = GrIntToScalar(r.fLeft);
216 rect->fTop = GrIntToScalar(r.fTop);
217 rect->fRight = GrIntToScalar(r.fRight);
218 rect->fBottom = GrIntToScalar(r.fBottom);
219 }
220
221 virtual GrPathIter* getPathIter() {
222 SkASSERT(0);
223 return NULL;
224 }
225
226 virtual GrPathFill getPathFill() const {
227 SkASSERT(0);
228 return kWinding_PathFill;
229 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000230private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000231 const SkRegion* fRegion;
232 SkRegion::Iterator fIter;
reed@google.comac10a2d2010-12-22 21:39:39 +0000233};
234
235class SkGlyphCache;
236
237class SkGrFontScaler : public GrFontScaler {
238public:
239 explicit SkGrFontScaler(SkGlyphCache* strike);
240 virtual ~SkGrFontScaler();
241
242 // overrides
243 virtual const GrKey* getKey();
reed@google.com98539c62011-03-15 15:40:16 +0000244 virtual GrMaskFormat getMaskFormat();
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds);
246 virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height,
247 int rowBytes, void* image);
248 virtual bool getGlyphPath(uint16_t glyphID, GrPath*);
249
250private:
251 SkGlyphCache* fStrike;
252 GrKey* fKey;
253// DECLARE_INSTANCE_COUNTER(SkGrFontScaler);
254};
255
256////////////////////////////////////////////////////////////////////////////////
257// Helper functions
258
bsalomon@google.com5782d712011-01-21 21:03:59 +0000259GrTextureEntry* sk_gr_create_bitmap_texture(GrContext* ctx,
reed@google.comac10a2d2010-12-22 21:39:39 +0000260 GrTextureKey* key,
261 const GrSamplerState& sampler,
262 const SkBitmap& bitmap);
263
reed@google.comac10a2d2010-12-22 21:39:39 +0000264
265#endif