blob: 4e9801f699d539ee5648c5c896b07bff85aa1f5c [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
23// tetrark headers
24#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"
36
37#if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG))
38// #error "inconsistent GR_DEBUG and SK_DEBUG"
39#endif
40
41#if GR_SCALAR_IS_FIXED
42 #ifdef SK_SCALAR_IS_FIXED
43 #define SK_SCALAR_IS_GR_SCALAR 1
44 #else
45 #define SK_SCALAR_IS_GR_SCALAR 0
46 #endif
47 #define SkScalarToGrScalar(x) SkScalarToFixed(x)
48
49#elif GR_SCALAR_IS_FLOAT
50
51 #ifdef SK_SCALAR_IS_FLOAT
52 #define SK_SCALAR_IS_GR_SCALAR 1
53 #else
54 #define SK_SCALAR_IS_GR_SCALAR 0
55 #endif
56 #define SkScalarToGrScalar(x) SkScalarToFloat(x)
57
58#else
59 #error "Ganesh scalar type not defined"
60#endif
61
62////////////////////////////////////////////////////////////////////////////////
63// Sk to Gr Type conversions
64
65// Verify that SkPoint and GrPoint are compatible if using the same scalar type
66#if 0/*SK_SCALAR_IS_GR_SCALAR*/
67 GR_STATIC_ASSERT(sizeof(SkPoint) == sizeof(GrPoint));
68 GR_STATIC_ASSERT(offsetof(SkPoint,fX) == offsetof(GrPoint,fX)));
69 GR_STATIC_ASSERT(offsetof(SkPoint,fY) == offsetof(GrPoint,fY)));
70#endif
71
72GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode);
73GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==(
74 int)SkShader::kRepeat_TileMode);
75GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode ==
76 (int)SkShader::kMirror_TileMode);
77
78#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X))
79
80GR_STATIC_ASSERT((int)GrGpu::kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff);
81GR_STATIC_ASSERT((int)GrGpu::kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff);
82GR_STATIC_ASSERT((int)GrGpu::kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff);
83GR_STATIC_ASSERT((int)GrGpu::kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff);
84GR_STATIC_ASSERT((int)GrGpu::kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff);
85GR_STATIC_ASSERT((int)GrGpu::kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff);
86GR_STATIC_ASSERT((int)GrGpu::kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff);
87GR_STATIC_ASSERT((int)GrGpu::kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff);
88GR_STATIC_ASSERT((int)GrGpu::kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff);
89GR_STATIC_ASSERT((int)GrGpu::kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff);
90
91#define sk_blend_to_grblend(X) ((GrGpu::BlendCoeff)(X))
92
93GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)GrPathIter::kMove_Command);
94GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)GrPathIter::kLine_Command);
95GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)GrPathIter::kQuadratic_Command);
96GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)GrPathIter::kCubic_Command);
97GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)GrPathIter::kClose_Command);
98GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)GrPathIter::kEnd_Command);
99
100#define sk_path_verb_to_gr_path_command(X) ((GrPathIter::Command)(X))
101
102///////////////////////////////////////////////////////////////////////////////
103
104#include "SkColorPriv.h"
105
106static inline GrRect Sk2Gr(const SkRect& src) {
107 return GrRect(SkScalarToGrScalar(src.fLeft),
108 SkScalarToGrScalar(src.fTop),
109 SkScalarToGrScalar(src.fRight),
110 SkScalarToGrScalar(src.fBottom));
111}
112
113class SkGr {
114public:
115 static inline SkIRect& SetIRect(SkIRect* dst, const GrIRect& src) {
116 GR_STATIC_ASSERT(sizeof(*dst) == sizeof(src));
117 memcpy(dst, &src, sizeof(*dst));
118 return *dst;
119 }
120
121 static inline GrIRect& SetIRect(GrIRect* dst, const SkIRect& src) {
122 GR_STATIC_ASSERT(sizeof(*dst) == sizeof(src));
123 memcpy(dst, &src, sizeof(*dst));
124 return *dst;
125 }
126
127 /**
128 * Convert the SkBitmap::Config to the corresponding PixelConfig, or
129 * kUnknown_PixelConfig if the conversion cannot be done.
130 */
131 static GrTexture::PixelConfig BitmapConfig2PixelConfig(SkBitmap::Config,
132 bool isOpaque);
133
134 static GrTexture::PixelConfig Bitmap2PixelConfig(const SkBitmap& bm) {
135 return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque());
136 }
137
138 static void SkMatrix2GrMatrix(const SkMatrix& m, GrMatrix* g) {
139 g->setAll(SkScalarToGrScalar(m[0]),
140 SkScalarToGrScalar(m[1]),
141 SkScalarToGrScalar(m[2]),
142 SkScalarToGrScalar(m[3]),
143 SkScalarToGrScalar(m[4]),
144 SkScalarToGrScalar(m[5]),
145 SkScalarToGrScalar(m[6]),
146 SkScalarToGrScalar(m[7]),
147 SkScalarToGrScalar(m[8]));
148 }
149
150 static GrColor SkColor2GrColor(SkColor c) {
151 SkPMColor pm = SkPreMultiplyColor(c);
152 unsigned r = SkGetPackedR32(pm);
153 unsigned g = SkGetPackedG32(pm);
154 unsigned b = SkGetPackedB32(pm);
155 unsigned a = SkGetPackedA32(pm);
156 return GrColorPackRGBA(r, g, b, a);
157 }
158
159 /**
160 * This abandons all texture caches (for bitmaps and text) associated with
161 * the gpu, and frees any associated skia caches. It differs from
162 * deleteAllTextures in that it assumes that the gpu has lots its context,
163 * and thus the associated HW textures are no longer valid
164 */
165 static void AbandonAllTextures(GrContext*);
166};
167
168////////////////////////////////////////////////////////////////////////////////
169// Classes
170
171class SkGrPathIter : public GrPathIter {
172public:
173 SkGrPathIter(const SkPath& path) : fIter(path, false), fPath(path) {}
174 virtual Command next(GrPoint pts[]);
175 virtual Command next();
176 virtual void rewind();
177 virtual ConvexHint hint() const;
178private:
179
180#if !SK_SCALAR_IS_GR_SCALAR
181 SkPoint fPoints[4];
182#endif
183 SkPath::Iter fIter;
184 const SkPath& fPath;
185};
186
187class SkGrClipIterator : public GrClipIterator {
188public:
189 void reset(const SkRegion& clip) {
190 fIter.reset(clip);
191 this->invalidateBoundsCache();
192 }
193
194 // overrides
195
196 virtual bool isDone() { return fIter.done(); }
197 virtual void getRect(GrIRect* rect) {
198 SkGr::SetIRect(rect, fIter.rect());
199 }
200 virtual void next() { fIter.next(); }
201 virtual void rewind() { fIter.rewind(); }
202 virtual void computeBounds(GrIRect* bounds);
203
204private:
205 SkRegion::Iterator fIter;
206};
207
208class SkGlyphCache;
209
210class SkGrFontScaler : public GrFontScaler {
211public:
212 explicit SkGrFontScaler(SkGlyphCache* strike);
213 virtual ~SkGrFontScaler();
214
215 // overrides
216 virtual const GrKey* getKey();
217 virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds);
218 virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height,
219 int rowBytes, void* image);
220 virtual bool getGlyphPath(uint16_t glyphID, GrPath*);
221
222private:
223 SkGlyphCache* fStrike;
224 GrKey* fKey;
225// DECLARE_INSTANCE_COUNTER(SkGrFontScaler);
226};
227
228////////////////////////////////////////////////////////////////////////////////
229// Helper functions
230
231GrTextureEntry* sk_gr_create_bitmap_texture(GrContext* ctx,
232 GrTextureKey* key,
233 const GrSamplerState& sampler,
234 const SkBitmap& bitmap);
235
236void sk_gr_set_paint(GrContext* ctx, const SkPaint& paint, bool justAlpha = false);
237
238#endif