blob: ad157d5dc3ba1e0e28609d9f7dca168f3a7cbf8d [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
bsalomon@google.com6a2134e2011-11-23 21:33:11 +000017#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#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
reed@google.comac10a2d2010-12-22 21:39:39 +000035////////////////////////////////////////////////////////////////////////////////
36// Sk to Gr Type conversions
37
reed@google.comac10a2d2010-12-22 21:39:39 +000038GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode);
39GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==(
40 int)SkShader::kRepeat_TileMode);
bsalomon@google.com5782d712011-01-21 21:03:59 +000041GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode ==
reed@google.comac10a2d2010-12-22 21:39:39 +000042 (int)SkShader::kMirror_TileMode);
43
44#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X))
45
bsalomon@google.comffca4002011-02-22 20:34:01 +000046GR_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);
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.comffca4002011-02-22 20:34:01 +000057#define sk_blend_to_grblend(X) ((GrBlendCoeff)(X))
reed@google.comac10a2d2010-12-22 21:39:39 +000058
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000059GR_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);
reed@google.comac10a2d2010-12-22 21:39:39 +000065
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000066#define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X))
reed@google.comac10a2d2010-12-22 21:39:39 +000067
68///////////////////////////////////////////////////////////////////////////////
69
70#include "SkColorPriv.h"
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072class SkGr {
73public:
reed@google.comac10a2d2010-12-22 21:39:39 +000074 /**
75 * Convert the SkBitmap::Config to the corresponding PixelConfig, or
76 * kUnknown_PixelConfig if the conversion cannot be done.
77 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +000078 static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config,
79 bool isOpaque);
reed@google.comac10a2d2010-12-22 21:39:39 +000080
bsalomon@google.com669fdc42011-04-05 17:08:27 +000081 static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) {
reed@google.comac10a2d2010-12-22 21:39:39 +000082 return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque());
83 }
84
reed@google.comac10a2d2010-12-22 21:39:39 +000085 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 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093};
94
95////////////////////////////////////////////////////////////////////////////////
96// Classes
97
reed@google.comac10a2d2010-12-22 21:39:39 +000098class SkGrClipIterator : public GrClipIterator {
99public:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000100 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 {
scroggo7b118072011-03-23 15:04:26 +0000114 if (!fCurr->fRect) {
115 rect->setEmpty();
116 } else {
reed@google.com20efde72011-05-09 17:00:02 +0000117 *rect = *fCurr->fRect;
scroggo7b118072011-03-23 15:04:26 +0000118 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119 }
120
reed@google.com07f3ee12011-05-16 17:21:57 +0000121 virtual const GrPath* getPath() {
122 return fCurr->fPath;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000123 }
124
125 virtual GrPathFill getPathFill() const;
126
127private:
128 const SkClipStack* fClipStack;
129 SkClipStack::B2FIter fIter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130 // 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
reed@google.com98539c62011-03-15 15:40:16 +0000140 void reset(const SkRegion& region) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000141 fRegion = &region;
142 fIter.reset(region);
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000144
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 // overrides
bsalomon@google.comd302f142011-03-03 13:54:13 +0000146 virtual bool isDone() const { return fIter.done(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 virtual void next() { fIter.next(); }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000148 virtual void rewind() { this->reset(*fRegion); }
149 virtual GrClipType getType() const { return kRect_ClipType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000150
bsalomon@google.comd302f142011-03-03 13:54:13 +0000151 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
reed@google.com07f3ee12011-05-16 17:21:57 +0000161 virtual const GrPath* getPath() {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000162 SkASSERT(0);
163 return NULL;
164 }
165
166 virtual GrPathFill getPathFill() const {
167 SkASSERT(0);
168 return kWinding_PathFill;
169 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000170private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000171 const SkRegion* fRegion;
172 SkRegion::Iterator fIter;
reed@google.comac10a2d2010-12-22 21:39:39 +0000173};
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();
reed@google.com98539c62011-03-15 15:40:16 +0000184 virtual GrMaskFormat getMaskFormat();
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 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
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199static 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);
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
reed@google.comac10a2d2010-12-22 21:39:39 +0000205
206#endif