blob: 892e279f59fbc7eb47ac7d333abd6fc60dfe56fd [file] [log] [blame]
kkinnunenc6cb56f2014-06-24 00:12:27 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrStencilAndCoverTextContext_DEFINED
9#define GrStencilAndCoverTextContext_DEFINED
10
cdaltonb2808cd2014-07-25 14:13:57 -070011#include "GrDrawTarget.h"
kkinnunen50b58e62015-05-18 23:02:07 -070012#include "GrStrokeInfo.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080013#include "SkDrawFilter.h"
14#include "SkTextBlob.h"
cdaltoncdd79072015-10-05 15:37:35 -070015#include "SkTHash.h"
16#include "SkTInternalLList.h"
17#include "SkTLList.h"
cdaltoncdd46822015-12-08 10:48:31 -080018#include "batches/GrDrawPathBatch.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070019
joshualitte55750e2016-02-10 12:52:21 -080020class GrAtlasTextContext;
kkinnunenc6cb56f2014-06-24 00:12:27 -070021class GrTextStrike;
22class GrPath;
robertphillipsfcf78292015-06-19 11:49:52 -070023class SkSurfaceProps;
kkinnunenc6cb56f2014-06-24 00:12:27 -070024
25/*
26 * This class implements text rendering using stencil and cover path rendering
27 * (by the means of GrDrawTarget::drawPath).
kkinnunenc6cb56f2014-06-24 00:12:27 -070028 */
joshualitt8e84a1e2016-02-16 11:09:25 -080029class GrStencilAndCoverTextContext {
kkinnunenc6cb56f2014-06-24 00:12:27 -070030public:
joshualitt27004b72016-02-11 12:00:33 -080031 static GrStencilAndCoverTextContext* Create();
jvanverth8c27a182014-10-14 08:45:50 -070032
joshualitt27004b72016-02-11 12:00:33 -080033 void drawText(GrContext*, GrDrawContext* dc,
joshualitte55750e2016-02-10 12:52:21 -080034 const GrClip&, const GrPaint&, const SkPaint&,
joshualitt2c89bc12016-02-11 05:42:30 -080035 const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
36 size_t byteLength, SkScalar x,
joshualitt8e84a1e2016-02-16 11:09:25 -080037 SkScalar y, const SkIRect& clipBounds);
joshualitt27004b72016-02-11 12:00:33 -080038 void drawPosText(GrContext*, GrDrawContext*,
joshualitte55750e2016-02-10 12:52:21 -080039 const GrClip&, const GrPaint&, const SkPaint&,
joshualitt2c89bc12016-02-11 05:42:30 -080040 const SkMatrix& viewMatrix, const SkSurfaceProps&,
joshualitte55750e2016-02-10 12:52:21 -080041 const char text[], size_t byteLength,
42 const SkScalar pos[], int scalarsPerPosition,
joshualitt8e84a1e2016-02-16 11:09:25 -080043 const SkPoint& offset, const SkIRect& clipBounds);
joshualitt27004b72016-02-11 12:00:33 -080044 void drawTextBlob(GrContext*, GrDrawContext*, const GrClip&, const SkPaint&,
joshualitt2c89bc12016-02-11 05:42:30 -080045 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkTextBlob*,
46 SkScalar x, SkScalar y,
joshualitt8e84a1e2016-02-16 11:09:25 -080047 SkDrawFilter*, const SkIRect& clipBounds);
joshualitte55750e2016-02-10 12:52:21 -080048
kkinnunenc6cb56f2014-06-24 00:12:27 -070049 virtual ~GrStencilAndCoverTextContext();
50
kkinnunenc6cb56f2014-06-24 00:12:27 -070051private:
joshualitt27004b72016-02-11 12:00:33 -080052 GrStencilAndCoverTextContext();
jvanverth8c27a182014-10-14 08:45:50 -070053
joshualitte55750e2016-02-10 12:52:21 -080054 bool canDraw(const SkPaint& skPaint, const SkMatrix&) {
robertphillips6ee690e2015-12-02 08:57:50 -080055 return this->internalCanDraw(skPaint);
56 }
cdaltoncdd79072015-10-05 15:37:35 -070057
58 bool internalCanDraw(const SkPaint&);
jvanverth8c27a182014-10-14 08:45:50 -070059
joshualitt27004b72016-02-11 12:00:33 -080060 void uncachedDrawTextBlob(GrContext*, GrDrawContext* dc,
joshualitte55750e2016-02-10 12:52:21 -080061 const GrClip& clip, const SkPaint& skPaint,
62 const SkMatrix& viewMatrix,
joshualitt2c89bc12016-02-11 05:42:30 -080063 const SkSurfaceProps&,
joshualitte55750e2016-02-10 12:52:21 -080064 const SkTextBlob* blob,
65 SkScalar x, SkScalar y,
66 SkDrawFilter* drawFilter,
67 const SkIRect& clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070068
cdalton02015e52015-10-05 15:28:20 -070069 class FallbackBlobBuilder;
70
cdalton3bd909a2015-10-05 14:57:20 -070071 class TextRun {
72 public:
73 TextRun(const SkPaint& fontAndStroke);
74 ~TextRun();
75
cdalton8585dd22015-10-08 08:04:09 -070076 void setText(const char text[], size_t byteLength, SkScalar x, SkScalar y);
cdalton3bd909a2015-10-05 14:57:20 -070077
cdalton8585dd22015-10-08 08:04:09 -070078 void setPosText(const char text[], size_t byteLength, const SkScalar pos[],
79 int scalarsPerPosition, const SkPoint& offset);
cdalton3bd909a2015-10-05 14:57:20 -070080
cdalton8585dd22015-10-08 08:04:09 -070081 void draw(GrContext*, GrDrawContext*, GrPipelineBuilder*, GrColor, const SkMatrix&,
joshualitt2c89bc12016-02-11 05:42:30 -080082 const SkSurfaceProps&,
cdaltoncdd79072015-10-05 15:37:35 -070083 SkScalar x, SkScalar y, const SkIRect& clipBounds,
joshualitt8e84a1e2016-02-16 11:09:25 -080084 GrAtlasTextContext* fallbackTextContext, const SkPaint& originalSkPaint) const;
cdaltoncdd79072015-10-05 15:37:35 -070085
cdalton8585dd22015-10-08 08:04:09 -070086 void releaseGlyphCache() const;
87
88 size_t computeSizeInCache() const;
cdalton3bd909a2015-10-05 14:57:20 -070089
90 private:
cdaltoncdd46822015-12-08 10:48:31 -080091 typedef GrDrawPathRangeBatch::InstanceData InstanceData;
92
cdalton8585dd22015-10-08 08:04:09 -070093 SkGlyphCache* getGlyphCache() const;
94 GrPathRange* createGlyphs(GrContext*) const;
cdalton02015e52015-10-05 15:28:20 -070095 void appendGlyph(const SkGlyph&, const SkPoint&, FallbackBlobBuilder*);
cdalton3bd909a2015-10-05 14:57:20 -070096
97 GrStrokeInfo fStroke;
98 SkPaint fFont;
99 SkScalar fTextRatio;
100 float fTextInverseRatio;
cdalton3bd909a2015-10-05 14:57:20 -0700101 bool fUsingRawGlyphPaths;
cdalton8585dd22015-10-08 08:04:09 -0700102 GrUniqueKey fGlyphPathsKey;
cdaltoncdd79072015-10-05 15:37:35 -0700103 int fTotalGlyphCount;
cdaltoncdd46822015-12-08 10:48:31 -0800104 SkAutoTUnref<InstanceData> fInstanceData;
105 int fFallbackGlyphCount;
cdalton02015e52015-10-05 15:28:20 -0700106 SkAutoTUnref<const SkTextBlob> fFallbackTextBlob;
cdalton8585dd22015-10-08 08:04:09 -0700107 mutable SkGlyphCache* fDetachedGlyphCache;
108 mutable uint32_t fLastDrawnGlyphsID;
cdalton3bd909a2015-10-05 14:57:20 -0700109 };
kkinnunenc6cb56f2014-06-24 00:12:27 -0700110
cdaltoncdd79072015-10-05 15:37:35 -0700111 // Text blobs/caches.
112
bsalomonf045d602015-11-18 19:01:12 -0800113 class TextBlob : public SkTLList<TextRun, 1> {
cdaltoncdd79072015-10-05 15:37:35 -0700114 public:
115 typedef SkTArray<uint32_t, true> Key;
116
117 static const Key& GetKey(const TextBlob* blob) { return blob->key(); }
118
119 static uint32_t Hash(const Key& key) {
120 SkASSERT(key.count() > 1); // 1-length keys should be using the blob-id hash map.
121 return SkChecksum::Murmur3(key.begin(), sizeof(uint32_t) * key.count());
122 }
123
cdalton8585dd22015-10-08 08:04:09 -0700124 TextBlob(uint32_t blobId, const SkTextBlob* skBlob, const SkPaint& skPaint)
125 : fKey(&blobId, 1) { this->init(skBlob, skPaint); }
cdaltoncdd79072015-10-05 15:37:35 -0700126
cdalton8585dd22015-10-08 08:04:09 -0700127 TextBlob(const Key& key, const SkTextBlob* skBlob, const SkPaint& skPaint)
cdaltoncdd79072015-10-05 15:37:35 -0700128 : fKey(key) {
129 // 1-length keys are unterstood to be the blob id and must use the other constructor.
130 SkASSERT(fKey.count() > 1);
cdalton8585dd22015-10-08 08:04:09 -0700131 this->init(skBlob, skPaint);
cdaltoncdd79072015-10-05 15:37:35 -0700132 }
133
134 const Key& key() const { return fKey; }
135
cdalton8585dd22015-10-08 08:04:09 -0700136 size_t cpuMemorySize() const { return fCpuMemorySize; }
cdaltoncdd79072015-10-05 15:37:35 -0700137
138 private:
cdalton8585dd22015-10-08 08:04:09 -0700139 void init(const SkTextBlob*, const SkPaint&);
cdaltoncdd79072015-10-05 15:37:35 -0700140
141 const SkSTArray<1, uint32_t, true> fKey;
cdalton8585dd22015-10-08 08:04:09 -0700142 size_t fCpuMemorySize;
cdaltoncdd79072015-10-05 15:37:35 -0700143
144 SK_DECLARE_INTERNAL_LLIST_INTERFACE(TextBlob);
145 };
146
147 const TextBlob& findOrCreateTextBlob(const SkTextBlob*, const SkPaint&);
148 void purgeToFit(const TextBlob&);
149
joshualitte55750e2016-02-10 12:52:21 -0800150 GrAtlasTextContext* fFallbackTextContext;
cdaltoncdd79072015-10-05 15:37:35 -0700151 SkTHashMap<uint32_t, TextBlob*> fBlobIdCache;
152 SkTHashTable<TextBlob*, const TextBlob::Key&, TextBlob> fBlobKeyCache;
153 SkTInternalLList<TextBlob> fLRUList;
cdalton8585dd22015-10-08 08:04:09 -0700154 size_t fCacheSize;
kkinnunenc6cb56f2014-06-24 00:12:27 -0700155};
156
157#endif