1
2/*
3 * 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.
7 */
8
9
10
11#ifndef SkGr_DEFINED
12#define SkGr_DEFINED
13
14#include <stddef.h>
15
16// Gr headers
17#include "GrTypes.h"
18#include "GrContext.h"
19#include "GrFontScaler.h"
20#include "GrClipIterator.h"
21#include "GrPath.h"
22
23// skia headers
24#include "SkBitmap.h"
25#include "SkPath.h"
26#include "SkPoint.h"
27#include "SkRegion.h"
28#include "SkShader.h"
29#include "SkClipStack.h"
30
31#if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG))
32//    #error "inconsistent GR_DEBUG and SK_DEBUG"
33#endif
34
35////////////////////////////////////////////////////////////////////////////////
36// Sk to Gr Type conversions
37
38GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode);
39GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==(
40                 int)SkShader::kRepeat_TileMode);
41GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode ==
42                 (int)SkShader::kMirror_TileMode);
43
44#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X))
45
46GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff);
47GR_STATIC_ASSERT((int)kOne_BlendCoeff  == (int)SkXfermode::kOne_Coeff);
48GR_STATIC_ASSERT((int)kSC_BlendCoeff   == (int)SkXfermode::kSC_Coeff);
49GR_STATIC_ASSERT((int)kISC_BlendCoeff  == (int)SkXfermode::kISC_Coeff);
50GR_STATIC_ASSERT((int)kDC_BlendCoeff   == (int)SkXfermode::kDC_Coeff);
51GR_STATIC_ASSERT((int)kIDC_BlendCoeff  == (int)SkXfermode::kIDC_Coeff);
52GR_STATIC_ASSERT((int)kSA_BlendCoeff   == (int)SkXfermode::kSA_Coeff);
53GR_STATIC_ASSERT((int)kISA_BlendCoeff  == (int)SkXfermode::kISA_Coeff);
54GR_STATIC_ASSERT((int)kDA_BlendCoeff   == (int)SkXfermode::kDA_Coeff);
55GR_STATIC_ASSERT((int)kIDA_BlendCoeff  == (int)SkXfermode::kIDA_Coeff);
56
57#define sk_blend_to_grblend(X) ((GrBlendCoeff)(X))
58
59GR_STATIC_ASSERT((int)SkPath::kMove_Verb  == (int)kMove_PathCmd);
60GR_STATIC_ASSERT((int)SkPath::kLine_Verb  == (int)kLine_PathCmd);
61GR_STATIC_ASSERT((int)SkPath::kQuad_Verb  == (int)kQuadratic_PathCmd);
62GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd);
63GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd);
64GR_STATIC_ASSERT((int)SkPath::kDone_Verb  == (int)kEnd_PathCmd);
65
66#define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X))
67
68///////////////////////////////////////////////////////////////////////////////
69
70#include "SkColorPriv.h"
71
72class SkGr {
73public:
74    /**
75     *  Convert the SkBitmap::Config to the corresponding PixelConfig, or
76     *  kUnknown_PixelConfig if the conversion cannot be done.
77     */
78    static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config,
79                                                  bool isOpaque);
80
81    static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) {
82        return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque());
83    }
84
85    static GrColor SkColor2GrColor(SkColor c) {
86        SkPMColor pm = SkPreMultiplyColor(c);
87        unsigned r = SkGetPackedR32(pm);
88        unsigned g = SkGetPackedG32(pm);
89        unsigned b = SkGetPackedB32(pm);
90        unsigned a = SkGetPackedA32(pm);
91        return GrColorPackRGBA(r, g, b, a);
92    }
93};
94
95////////////////////////////////////////////////////////////////////////////////
96// Classes
97
98class SkGrClipIterator : public GrClipIterator {
99public:
100    SkGrClipIterator() { fClipStack = NULL;  fCurr = NULL; }
101    SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); }
102
103    void reset(const SkClipStack& clipStack);
104
105    // overrides
106    virtual bool isDone() const { return NULL == fCurr; }
107    virtual void next() { fCurr = fIter.next(); }
108    virtual void rewind() { this->reset(*fClipStack); }
109    virtual GrClipType getType() const;
110
111    virtual GrSetOp getOp() const;
112
113    virtual void getRect(GrRect* rect) const {
114        if (!fCurr->fRect) {
115            rect->setEmpty();
116        } else {
117            *rect = *fCurr->fRect;
118        }
119    }
120
121    virtual const GrPath* getPath() {
122        return fCurr->fPath;
123    }
124
125    virtual GrPathFill getPathFill() const;
126
127private:
128    const SkClipStack*                  fClipStack;
129    SkClipStack::B2FIter                fIter;
130    // SkClipStack's auto advances on each get
131    // so we store the current pos here.
132    const SkClipStack::B2FIter::Clip*   fCurr;
133};
134
135class SkGrRegionIterator : public GrClipIterator {
136public:
137    SkGrRegionIterator() {}
138    SkGrRegionIterator(const SkRegion& region) { this->reset(region); }
139
140    void reset(const SkRegion& region) {
141        fRegion = &region;
142        fIter.reset(region);
143    }
144
145    // overrides
146    virtual bool isDone() const { return fIter.done(); }
147    virtual void next() { fIter.next(); }
148    virtual void rewind() { this->reset(*fRegion); }
149    virtual GrClipType getType() const { return kRect_ClipType; }
150
151    virtual GrSetOp getOp() const { return kUnion_SetOp; }
152
153    virtual void getRect(GrRect* rect) const {
154        const SkIRect& r = fIter.rect();
155        rect->fLeft   = GrIntToScalar(r.fLeft);
156        rect->fTop    = GrIntToScalar(r.fTop);
157        rect->fRight  = GrIntToScalar(r.fRight);
158        rect->fBottom = GrIntToScalar(r.fBottom);
159    }
160
161    virtual const GrPath* getPath() {
162        SkASSERT(0);
163        return NULL;
164    }
165
166    virtual GrPathFill getPathFill() const {
167        SkASSERT(0);
168        return kWinding_PathFill;
169    }
170private:
171    const SkRegion*     fRegion;
172    SkRegion::Iterator  fIter;
173};
174
175class SkGlyphCache;
176
177class SkGrFontScaler : public GrFontScaler {
178public:
179    explicit SkGrFontScaler(SkGlyphCache* strike);
180    virtual ~SkGrFontScaler();
181
182    // overrides
183    virtual const GrKey* getKey();
184    virtual GrMaskFormat getMaskFormat();
185    virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds);
186    virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height,
187                                     int rowBytes, void* image);
188    virtual bool getGlyphPath(uint16_t glyphID, GrPath*);
189
190private:
191    SkGlyphCache* fStrike;
192    GrKey*  fKey;
193//    DECLARE_INSTANCE_COUNTER(SkGrFontScaler);
194};
195
196////////////////////////////////////////////////////////////////////////////////
197// Helper functions
198
199static const GrContext::TextureKey gUNCACHED_KEY = ~0;
200GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx,
201                                                GrContext::TextureKey key,
202                                                const GrSamplerState* sampler,
203                                                const SkBitmap& bitmap);
204
205
206#endif
207