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