reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrTextContext_DEFINED |
| 9 | #define GrTextContext_DEFINED |
| 10 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 11 | #include "GrPoint.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "GrGlyph.h" |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 13 | #include "GrPaint.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 14 | #include "SkDeviceProperties.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 16 | #include "SkPostConfig.h" |
| 17 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | class GrContext; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 19 | class GrDrawTarget; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 20 | class GrFontScaler; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 22 | /* |
| 23 | * This class wraps the state for a single text render |
| 24 | */ |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 25 | class GrTextContext { |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 26 | public: |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 27 | virtual ~GrTextContext() {} |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 28 | virtual void drawText(const char text[], size_t byteLength, SkScalar x, SkScalar y) = 0; |
| 29 | virtual void drawPosText(const char text[], size_t byteLength, |
| 30 | const SkScalar pos[], SkScalar constY, |
| 31 | int scalarsPerPosition) = 0; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 32 | |
| 33 | protected: |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 34 | GrTextContext(GrContext*, const GrPaint&, const SkPaint&, const SkDeviceProperties&); |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 36 | static GrFontScaler* GetGrFontScaler(SkGlyphCache* cache); |
| 37 | static void MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
| 38 | const char text[], size_t byteLength, SkVector* stopVector); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame^] | 39 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 40 | GrContext* fContext; |
| 41 | GrPaint fPaint; |
| 42 | SkPaint fSkPaint; |
| 43 | SkDeviceProperties fDeviceProperties; |
| 44 | GrDrawTarget* fDrawTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 46 | SkIRect fClipRect; |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * These classes wrap the creation of a single text context for a given GPU device. The |
| 51 | * assumption is that we'll only be using one text context at a time for that device. |
| 52 | */ |
| 53 | class GrTextContextManager { |
| 54 | public: |
| 55 | virtual ~GrTextContextManager() {} |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 56 | virtual GrTextContext* create(GrContext* grContext, const GrPaint& grPaint, |
| 57 | const SkPaint& skPaint, const SkDeviceProperties& props) = 0; |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | template <class TextContextClass> |
| 61 | class GrTTextContextManager : public GrTextContextManager { |
| 62 | private: |
| 63 | class ManagedTextContext : public TextContextClass { |
| 64 | public: |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 65 | virtual ~ManagedTextContext() {} |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 67 | ManagedTextContext(GrContext* grContext, |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 68 | const GrPaint& grPaint, |
| 69 | const SkPaint& skPaint, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 70 | const SkDeviceProperties& properties, |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 71 | GrTTextContextManager<TextContextClass>* manager) : |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 72 | TextContextClass(grContext, grPaint, skPaint, properties) { |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 73 | fManager = manager; |
| 74 | } |
| 75 | |
| 76 | static void operator delete(void* ptr) { |
| 77 | if (ptr == NULL) { |
| 78 | return; |
| 79 | } |
| 80 | ManagedTextContext* context = reinterpret_cast<ManagedTextContext*>(ptr); |
| 81 | context->fManager->recycle(context); |
| 82 | } |
| 83 | |
| 84 | static void operator delete(void*, void*) { |
| 85 | } |
| 86 | |
| 87 | GrTTextContextManager<TextContextClass>* fManager; |
| 88 | }; |
skia.committer@gmail.com | d2ac07b | 2014-01-25 07:01:49 +0000 | [diff] [blame] | 89 | |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 90 | public: |
| 91 | GrTTextContextManager() { |
| 92 | fAllocation = sk_malloc_throw(sizeof(ManagedTextContext)); |
| 93 | fUsed = false; |
| 94 | } |
| 95 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 96 | virtual ~GrTTextContextManager() { |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 97 | SkASSERT(!fUsed); |
| 98 | sk_free(fAllocation); |
| 99 | } |
| 100 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 101 | virtual GrTextContext* create(GrContext* grContext, const GrPaint& grPaint, |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame^] | 102 | const SkPaint& skPaint, const SkDeviceProperties& properties) |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 103 | SK_OVERRIDE { |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 104 | // add check for usePath here? |
| 105 | SkASSERT(!fUsed); |
| 106 | ManagedTextContext* obj = SkNEW_PLACEMENT_ARGS(fAllocation, ManagedTextContext, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 107 | (grContext, grPaint, skPaint, properties, |
| 108 | this)); |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 109 | fUsed = true; |
| 110 | return obj; |
| 111 | } |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
commit-bot@chromium.org | cc40f06 | 2014-01-24 14:38:27 +0000 | [diff] [blame] | 114 | void recycle(GrTextContext* textContext) { |
| 115 | SkASSERT((void*)textContext == fAllocation); |
| 116 | SkASSERT(fUsed); |
| 117 | fUsed = false; |
| 118 | } |
| 119 | |
| 120 | void* fAllocation; |
| 121 | bool fUsed; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | #endif |