blob: 3c61cbc80e25821405ba9dddb29135ae314c3f76 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrTextContext_DEFINED
9#define GrTextContext_DEFINED
10
bsalomon@google.com858804d2012-10-15 14:25:50 +000011#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrGlyph.h"
tomhudson@google.com375ff852012-06-29 18:37:57 +000013#include "GrPaint.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
commit-bot@chromium.orgce96b652014-01-22 21:37:03 +000015#include "SkPostConfig.h"
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017class GrContext;
tomhudson@google.com375ff852012-06-29 18:37:57 +000018class GrDrawTarget;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000019class GrFontScaler;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000021/*
22 * This class wraps the state for a single text render
23 */
tomhudson@google.com375ff852012-06-29 18:37:57 +000024class GrTextContext {
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000025public:
commit-bot@chromium.orgce96b652014-01-22 21:37:03 +000026 virtual ~GrTextContext() {}
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000027 virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
28 GrFontScaler*) = 0;
29
30protected:
commit-bot@chromium.orgce96b652014-01-22 21:37:03 +000031 GrTextContext(GrContext*, const GrPaint&, const SkPaint&);
reed@google.comfa35e3d2012-06-26 20:16:17 +000032
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000033 GrPaint fPaint;
commit-bot@chromium.orgce96b652014-01-22 21:37:03 +000034 SkPaint fSkPaint;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000035 GrContext* fContext;
36 GrDrawTarget* fDrawTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +000037
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000038 SkIRect fClipRect;
commit-bot@chromium.orgce96b652014-01-22 21:37:03 +000039};
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 */
45class GrTextContextManager {
46public:
47 virtual ~GrTextContextManager() {}
48 virtual GrTextContext* create(GrContext* context, const GrPaint& grPaint,
49 const SkPaint& skPaint) = 0;
50};
51
52template <class TextContextClass>
53class GrTTextContextManager : public GrTextContextManager {
54private:
55 class ManagedTextContext : public TextContextClass {
56 public:
57 ~ManagedTextContext() {}
58
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 GrTTextContextManager<TextContextClass>* fManager;
76 };
77
78public:
79 GrTTextContextManager() {
80 fAllocation = sk_malloc_throw(sizeof(ManagedTextContext));
81 fUsed = false;
82 }
83
84 ~GrTTextContextManager() {
85 SkASSERT(!fUsed);
86 sk_free(fAllocation);
87 }
88
89 GrTextContext* create(GrContext* context, const GrPaint& grPaint,
90 const SkPaint& skPaint) {
91 // add check for usePath here?
92 SkASSERT(!fUsed);
93 ManagedTextContext* obj = SkNEW_PLACEMENT_ARGS(fAllocation, ManagedTextContext,
94 (context, grPaint, skPaint, this));
95 fUsed = true;
96 return obj;
97 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000098
99private:
commit-bot@chromium.orgce96b652014-01-22 21:37:03 +0000100 void recycle(GrTextContext* textContext) {
101 SkASSERT((void*)textContext == fAllocation);
102 SkASSERT(fUsed);
103 fUsed = false;
104 }
105
106 void* fAllocation;
107 bool fUsed;
reed@google.comac10a2d2010-12-22 21:39:39 +0000108};
109
110#endif