| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrBitmapTextContext_DEFINED | 8 #ifndef GrBitmapTextContext_DEFINED |
| 9 #define GrBitmapTextContext_DEFINED | 9 #define GrBitmapTextContext_DEFINED |
| 10 | 10 |
| 11 #include "GrTextContext.h" | 11 #include "GrTextContext.h" |
| 12 | 12 |
| 13 #include "GrGeometryProcessor.h" | 13 #include "GrGeometryProcessor.h" |
| 14 #include "GrMemoryPool.h" |
| 15 #include "SkDescriptor.h" |
| 14 #include "SkTHash.h" | 16 #include "SkTHash.h" |
| 15 | 17 |
| 16 class GrBatchTextStrike; | 18 class GrBatchTextStrike; |
| 17 class GrPipelineBuilder; | 19 class GrPipelineBuilder; |
| 18 | 20 |
| 19 /* | 21 /* |
| 20 * This class implements GrTextContext using standard bitmap fonts, and can also
process textblobs. | 22 * This class implements GrTextContext using standard bitmap fonts, and can also
process textblobs. |
| 21 * TODO replace GrBitmapTextContext | 23 * TODO replace GrBitmapTextContext |
| 22 */ | 24 */ |
| 23 class GrBitmapTextContextB : public GrTextContext { | 25 class GrBitmapTextContextB : public GrTextContext { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 * the GrAtlas will not evict anything the Blob needs. | 60 * the GrAtlas will not evict anything the Blob needs. |
| 59 * TODO this is currently a bug | 61 * TODO this is currently a bug |
| 60 */ | 62 */ |
| 61 struct BitmapTextBlob : public SkRefCnt { | 63 struct BitmapTextBlob : public SkRefCnt { |
| 62 // Each Run inside of the blob can have its texture coordinates regenera
ted if required. | 64 // Each Run inside of the blob can have its texture coordinates regenera
ted if required. |
| 63 // To determine if regeneration is necessary, fAtlasGeneration is used.
If there have been | 65 // To determine if regeneration is necessary, fAtlasGeneration is used.
If there have been |
| 64 // any evictions inside of the atlas, then we will simply regenerate Run
s. We could track | 66 // any evictions inside of the atlas, then we will simply regenerate Run
s. We could track |
| 65 // this at a more fine grained level, but its not clear if this is worth
it, as evictions | 67 // this at a more fine grained level, but its not clear if this is worth
it, as evictions |
| 66 // should be fairly rare. | 68 // should be fairly rare. |
| 67 // One additional point, each run can contain glyphs with any of the thr
ee mask formats. | 69 // One additional point, each run can contain glyphs with any of the thr
ee mask formats. |
| 68 // We maintain separate arrays for each format type, and flush them sepa
rately. In practice | 70 // We call these SubRuns. Because a subrun must be a contiguous range,
we have to create |
| 69 // most of the time a run will have the same format type | 71 // a new subrun each time the mask format changes in a run. In theory,
a run can have as |
| 72 // many SubRuns as it has glyphs, ie if a run alternates between color e
moji and A8. In |
| 73 // practice, the vast majority of runs have only a single subrun. |
| 74 |
| 70 struct Run { | 75 struct Run { |
| 71 Run() : fColor(GrColor_ILLEGAL) { fVertexBounds.setLargestInverted()
; } | 76 Run() : fColor(GrColor_ILLEGAL), fInitialized(false) { |
| 72 struct PerFormatInfo { | 77 fVertexBounds.setLargestInverted(); |
| 73 PerFormatInfo() : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGe
neration) {} | 78 // We insert the first subrun to gurantee a run always has atlea
st one subrun. |
| 74 SkTDArray<unsigned char> fVertices; | 79 // We do this to simplify things when we 'hand off' data from on
e subrun to the |
| 75 SkTDArray<GrGlyph::PackedID> fGlyphIDs; | 80 // next |
| 81 fSubRunInfo.push_back(); |
| 82 } |
| 83 struct SubRunInfo { |
| 84 SubRunInfo() |
| 85 : fAtlasGeneration(GrBatchAtlas::kInvalidAtlasGeneration) |
| 86 , fGlyphStartIndex(0) |
| 87 , fGlyphEndIndex(0) |
| 88 , fVertexStartIndex(0) |
| 89 , fVertexEndIndex(0) {} |
| 90 GrMaskFormat fMaskFormat; |
| 76 uint64_t fAtlasGeneration; | 91 uint64_t fAtlasGeneration; |
| 92 uint32_t fGlyphStartIndex; |
| 93 uint32_t fGlyphEndIndex; |
| 94 size_t fVertexStartIndex; |
| 95 size_t fVertexEndIndex; |
| 77 }; | 96 }; |
| 78 SkAutoTUnref<const SkData> fDescriptor; | 97 SkSTArray<1, SubRunInfo, true> fSubRunInfo; |
| 98 SkAutoDescriptor fDescriptor; |
| 79 SkAutoTUnref<SkTypeface> fTypeface; | 99 SkAutoTUnref<SkTypeface> fTypeface; |
| 80 PerFormatInfo fInfos[kMaskFormatCount]; | |
| 81 SkRect fVertexBounds; | 100 SkRect fVertexBounds; |
| 82 GrColor fColor; | 101 GrColor fColor; |
| 102 bool fInitialized; |
| 83 }; | 103 }; |
| 84 SkSTArray<1, Run, true> fRuns; | 104 |
| 85 struct BigGlyph { | 105 struct BigGlyph { |
| 86 BigGlyph(const SkPath& path, int vx, int vy) : fPath(path), fVx(vx),
fVy(vy) {} | 106 BigGlyph(const SkPath& path, int vx, int vy) : fPath(path), fVx(vx),
fVy(vy) {} |
| 87 SkPath fPath; | 107 SkPath fPath; |
| 88 int fVx; | 108 int fVx; |
| 89 int fVy; | 109 int fVy; |
| 90 }; | 110 }; |
| 111 |
| 91 SkTArray<BigGlyph> fBigGlyphs; | 112 SkTArray<BigGlyph> fBigGlyphs; |
| 92 SkTextBlob* fBlob; | |
| 93 SkMatrix fViewMatrix; | 113 SkMatrix fViewMatrix; |
| 94 SkScalar fX; | 114 SkScalar fX; |
| 95 SkScalar fY; | 115 SkScalar fY; |
| 96 SkPaint::Style fStyle; | 116 SkPaint::Style fStyle; |
| 117 uint32_t fRunCount; |
| 118 |
| 119 // all glyph / vertex offsets are into these pools. |
| 120 unsigned char* fVertices; |
| 121 GrGlyph::PackedID* fGlyphIDs; |
| 122 Run* fRuns; |
| 97 | 123 |
| 98 static uint32_t Hash(const uint32_t& key) { | 124 static uint32_t Hash(const uint32_t& key) { |
| 99 return SkChecksum::Mix(key); | 125 return SkChecksum::Mix(key); |
| 100 } | 126 } |
| 127 |
| 128 void operator delete(void* p) { sk_free(p); } |
| 129 void* operator new(size_t) { |
| 130 SkFAIL("All blobs are created by placement new."); |
| 131 return sk_malloc_throw(0); |
| 132 } |
| 133 |
| 134 void* operator new(size_t, void* p) { return p; } |
| 135 void operator delete(void* target, void* placement) { |
| 136 ::operator delete(target, placement); |
| 137 } |
| 101 }; | 138 }; |
| 102 | 139 |
| 140 typedef BitmapTextBlob::Run Run; |
| 141 typedef Run::SubRunInfo PerSubRunInfo; |
| 142 |
| 143 BitmapTextBlob* CreateBlob(int glyphCount, int runCount); |
| 144 |
| 103 void appendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left,
int top, | 145 void appendGlyph(BitmapTextBlob*, int runIndex, GrGlyph::PackedID, int left,
int top, |
| 104 GrFontScaler*, const SkIRect& clipRect); | 146 GrFontScaler*, const SkIRect& clipRect); |
| 105 void flushSubRun(GrDrawTarget*, BitmapTextBlob*, int i, GrPipelineBuilder*,
GrMaskFormat, | |
| 106 GrColor color, int paintAlpha); | |
| 107 void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const GrPaint&,
const GrClip&, | 147 void flush(GrDrawTarget*, BitmapTextBlob*, GrRenderTarget*, const GrPaint&,
const GrClip&, |
| 108 const SkMatrix& viewMatrix, int paintAlpha); | 148 const SkMatrix& viewMatrix, int paintAlpha); |
| 109 | 149 |
| 110 void internalDrawText(BitmapTextBlob*, int runIndex, const SkPaint&, | 150 void internalDrawText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const Sk
Paint&, |
| 111 const SkMatrix& viewMatrix, const char text[], size_t
byteLength, | 151 const SkMatrix& viewMatrix, const char text[], size_t
byteLength, |
| 112 SkScalar x, SkScalar y, const SkIRect& clipRect); | 152 SkScalar x, SkScalar y, const SkIRect& clipRect); |
| 113 void internalDrawPosText(BitmapTextBlob*, int runIndex, const SkPaint&, | 153 void internalDrawPosText(BitmapTextBlob*, int runIndex, SkGlyphCache*, const
SkPaint&, |
| 114 const SkMatrix& viewMatrix, | 154 const SkMatrix& viewMatrix, |
| 115 const char text[], size_t byteLength, | 155 const char text[], size_t byteLength, |
| 116 const SkScalar pos[], int scalarsPerPosition, | 156 const SkScalar pos[], int scalarsPerPosition, |
| 117 const SkPoint& offset, const SkIRect& clipRect); | 157 const SkPoint& offset, const SkIRect& clipRect); |
| 118 | 158 |
| 159 // sets up the descriptor on the blob and returns a detached cache. Client
must attach |
| 160 inline SkGlyphCache* setupCache(Run*, const SkPaint&, const SkMatrix& viewMa
trix); |
| 119 static inline bool MustRegenerateBlob(const BitmapTextBlob&, const SkPaint&, | 161 static inline bool MustRegenerateBlob(const BitmapTextBlob&, const SkPaint&, |
| 120 const SkMatrix& viewMatrix, SkScalar x
, SkScalar y); | 162 const SkMatrix& viewMatrix, SkScalar x
, SkScalar y); |
| 121 void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, const S
kMatrix& viewMatrix, | 163 void regenerateTextBlob(BitmapTextBlob* bmp, const SkPaint& skPaint, const S
kMatrix& viewMatrix, |
| 122 const SkTextBlob* blob, SkScalar x, SkScalar y, | 164 const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 123 SkDrawFilter* drawFilter, const SkIRect& clipRect); | 165 SkDrawFilter* drawFilter, const SkIRect& clipRect); |
| 124 | 166 |
| 167 // TODO this currently only uses the public interface of SkTextBlob, however
, I may need to add |
| 168 // functionality to it while looping over the runs so I'm putting this here
for the time being. |
| 169 // If this lands in Chrome without changes, move it to SkTextBlob. |
| 170 static inline void BlobGlyphCount(int* glyphCount, int* runCount, const SkTe
xtBlob*); |
| 171 |
| 125 GrBatchTextStrike* fCurrStrike; | 172 GrBatchTextStrike* fCurrStrike; |
| 126 | 173 |
| 127 // TODO use real cache | 174 // TODO use real cache |
| 128 static void ClearCacheEntry(uint32_t key, BitmapTextBlob**); | 175 static void ClearCacheEntry(uint32_t key, BitmapTextBlob**); |
| 129 SkTHashMap<uint32_t, BitmapTextBlob*, BitmapTextBlob::Hash> fCache; | 176 SkTHashMap<uint32_t, BitmapTextBlob*, BitmapTextBlob::Hash> fCache; |
| 130 | 177 |
| 131 friend class BitmapTextBatch; | 178 friend class BitmapTextBatch; |
| 132 | 179 |
| 133 typedef GrTextContext INHERITED; | 180 typedef GrTextContext INHERITED; |
| 134 }; | 181 }; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 221 |
| 175 void init(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&, | 222 void init(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&, |
| 176 const SkIRect& regionClipBounds); | 223 const SkIRect& regionClipBounds); |
| 177 void appendGlyph(GrGlyph::PackedID, SkFixed left, SkFixed top, GrFontScaler*
); | 224 void appendGlyph(GrGlyph::PackedID, SkFixed left, SkFixed top, GrFontScaler*
); |
| 178 bool uploadGlyph(GrGlyph*, GrFontScaler*); | 225 bool uploadGlyph(GrGlyph*, GrFontScaler*); |
| 179 void flush(); // automatically called by destructor | 226 void flush(); // automatically called by destructor |
| 180 void finish(); | 227 void finish(); |
| 181 }; | 228 }; |
| 182 | 229 |
| 183 #endif | 230 #endif |
| OLD | NEW |