blob: 4cf19ec91122ff398b013b21d8a1416b64fe1564 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef SkGr_DEFINED
12#define SkGr_DEFINED
13
14#include <stddef.h>
15
bsalomon@google.comd302f142011-03-03 13:54:13 +000016// Gr headers
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrConfig.h"
18#include "GrContext.h"
19#include "GrFontScaler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "GrClipIterator.h"
reed@google.com07f3ee12011-05-16 17:21:57 +000021#include "GrPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
23// skia headers
24#include "SkBitmap.h"
25#include "SkPath.h"
26#include "SkPoint.h"
27#include "SkRegion.h"
28#include "SkShader.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000029#include "SkClipStack.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000030
31#if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG))
32// #error "inconsistent GR_DEBUG and SK_DEBUG"
33#endif
34
35#if GR_SCALAR_IS_FIXED
36 #ifdef SK_SCALAR_IS_FIXED
37 #define SK_SCALAR_IS_GR_SCALAR 1
38 #else
39 #define SK_SCALAR_IS_GR_SCALAR 0
40 #endif
41 #define SkScalarToGrScalar(x) SkScalarToFixed(x)
42
43#elif GR_SCALAR_IS_FLOAT
44
45 #ifdef SK_SCALAR_IS_FLOAT
46 #define SK_SCALAR_IS_GR_SCALAR 1
47 #else
48 #define SK_SCALAR_IS_GR_SCALAR 0
49 #endif
50 #define SkScalarToGrScalar(x) SkScalarToFloat(x)
51
bsalomon@google.com5782d712011-01-21 21:03:59 +000052#else
reed@google.comac10a2d2010-12-22 21:39:39 +000053 #error "Ganesh scalar type not defined"
54#endif
55
56////////////////////////////////////////////////////////////////////////////////
57// Sk to Gr Type conversions
58
59// Verify that SkPoint and GrPoint are compatible if using the same scalar type
60#if 0/*SK_SCALAR_IS_GR_SCALAR*/
61 GR_STATIC_ASSERT(sizeof(SkPoint) == sizeof(GrPoint));
62 GR_STATIC_ASSERT(offsetof(SkPoint,fX) == offsetof(GrPoint,fX)));
63 GR_STATIC_ASSERT(offsetof(SkPoint,fY) == offsetof(GrPoint,fY)));
64#endif
65
66GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode);
67GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==(
68 int)SkShader::kRepeat_TileMode);
bsalomon@google.com5782d712011-01-21 21:03:59 +000069GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode ==
reed@google.comac10a2d2010-12-22 21:39:39 +000070 (int)SkShader::kMirror_TileMode);
71
72#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X))
73
bsalomon@google.comffca4002011-02-22 20:34:01 +000074GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff);
75GR_STATIC_ASSERT((int)kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff);
76GR_STATIC_ASSERT((int)kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff);
77GR_STATIC_ASSERT((int)kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff);
78GR_STATIC_ASSERT((int)kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff);
79GR_STATIC_ASSERT((int)kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff);
80GR_STATIC_ASSERT((int)kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff);
81GR_STATIC_ASSERT((int)kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff);
82GR_STATIC_ASSERT((int)kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff);
83GR_STATIC_ASSERT((int)kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff);
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.comffca4002011-02-22 20:34:01 +000085#define sk_blend_to_grblend(X) ((GrBlendCoeff)(X))
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000087GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)kMove_PathCmd);
88GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)kLine_PathCmd);
89GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)kQuadratic_PathCmd);
90GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd);
91GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd);
92GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)kEnd_PathCmd);
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000094#define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X))
reed@google.comac10a2d2010-12-22 21:39:39 +000095
96///////////////////////////////////////////////////////////////////////////////
97
98#include "SkColorPriv.h"
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100class SkGr {
101public:
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 /**
103 * Convert the SkBitmap::Config to the corresponding PixelConfig, or
104 * kUnknown_PixelConfig if the conversion cannot be done.
105 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000106 static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config,
107 bool isOpaque);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000109 static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque());
111 }
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 static GrColor SkColor2GrColor(SkColor c) {
114 SkPMColor pm = SkPreMultiplyColor(c);
115 unsigned r = SkGetPackedR32(pm);
116 unsigned g = SkGetPackedG32(pm);
117 unsigned b = SkGetPackedB32(pm);
118 unsigned a = SkGetPackedA32(pm);
119 return GrColorPackRGBA(r, g, b, a);
120 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000121};
122
123////////////////////////////////////////////////////////////////////////////////
124// Classes
125
reed@google.comac10a2d2010-12-22 21:39:39 +0000126class SkGrClipIterator : public GrClipIterator {
127public:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000128 SkGrClipIterator() { fClipStack = NULL; fCurr = NULL; }
129 SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); }
130
131 void reset(const SkClipStack& clipStack);
132
133 // overrides
134 virtual bool isDone() const { return NULL == fCurr; }
135 virtual void next() { fCurr = fIter.next(); }
136 virtual void rewind() { this->reset(*fClipStack); }
137 virtual GrClipType getType() const;
138
139 virtual GrSetOp getOp() const;
140
141 virtual void getRect(GrRect* rect) const {
scroggo7b118072011-03-23 15:04:26 +0000142 if (!fCurr->fRect) {
143 rect->setEmpty();
144 } else {
reed@google.com20efde72011-05-09 17:00:02 +0000145 *rect = *fCurr->fRect;
scroggo7b118072011-03-23 15:04:26 +0000146 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000147 }
148
reed@google.com07f3ee12011-05-16 17:21:57 +0000149 virtual const GrPath* getPath() {
150 return fCurr->fPath;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000151 }
152
153 virtual GrPathFill getPathFill() const;
154
155private:
156 const SkClipStack* fClipStack;
157 SkClipStack::B2FIter fIter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000158 // SkClipStack's auto advances on each get
159 // so we store the current pos here.
160 const SkClipStack::B2FIter::Clip* fCurr;
161};
162
163class SkGrRegionIterator : public GrClipIterator {
164public:
165 SkGrRegionIterator() {}
166 SkGrRegionIterator(const SkRegion& region) { this->reset(region); }
167
reed@google.com98539c62011-03-15 15:40:16 +0000168 void reset(const SkRegion& region) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000169 fRegion = &region;
170 fIter.reset(region);
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000172
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 // overrides
bsalomon@google.comd302f142011-03-03 13:54:13 +0000174 virtual bool isDone() const { return fIter.done(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 virtual void next() { fIter.next(); }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000176 virtual void rewind() { this->reset(*fRegion); }
177 virtual GrClipType getType() const { return kRect_ClipType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
bsalomon@google.comd302f142011-03-03 13:54:13 +0000179 virtual GrSetOp getOp() const { return kUnion_SetOp; }
180
181 virtual void getRect(GrRect* rect) const {
182 const SkIRect& r = fIter.rect();
183 rect->fLeft = GrIntToScalar(r.fLeft);
184 rect->fTop = GrIntToScalar(r.fTop);
185 rect->fRight = GrIntToScalar(r.fRight);
186 rect->fBottom = GrIntToScalar(r.fBottom);
187 }
188
reed@google.com07f3ee12011-05-16 17:21:57 +0000189 virtual const GrPath* getPath() {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000190 SkASSERT(0);
191 return NULL;
192 }
193
194 virtual GrPathFill getPathFill() const {
195 SkASSERT(0);
196 return kWinding_PathFill;
197 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000198private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000199 const SkRegion* fRegion;
200 SkRegion::Iterator fIter;
reed@google.comac10a2d2010-12-22 21:39:39 +0000201};
202
203class SkGlyphCache;
204
205class SkGrFontScaler : public GrFontScaler {
206public:
207 explicit SkGrFontScaler(SkGlyphCache* strike);
208 virtual ~SkGrFontScaler();
209
210 // overrides
211 virtual const GrKey* getKey();
reed@google.com98539c62011-03-15 15:40:16 +0000212 virtual GrMaskFormat getMaskFormat();
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds);
214 virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height,
215 int rowBytes, void* image);
216 virtual bool getGlyphPath(uint16_t glyphID, GrPath*);
217
218private:
219 SkGlyphCache* fStrike;
220 GrKey* fKey;
221// DECLARE_INSTANCE_COUNTER(SkGrFontScaler);
222};
223
224////////////////////////////////////////////////////////////////////////////////
225// Helper functions
226
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000227static const GrContext::TextureKey gUNCACHED_KEY = ~0;
228GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx,
229 GrContext::TextureKey key,
230 const GrSamplerState& sampler,
231 const SkBitmap& bitmap);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
234#endif