Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrTextContext.h
1 /*
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.
6  */
7
8 #ifndef GrTextContext_DEFINED
9 #define GrTextContext_DEFINED
10
11 #include "GrContext.h"
12 #include "GrGlyph.h"
13 #include "GrPaint.h"
14
15 #include "SkPostConfig.h"
16
17 class GrContext;
18 class GrDrawTarget;
19 class GrFontScaler;
20
21 /*
22  * This class wraps the state for a single text render
23  */
24 class GrTextContext {
25 public:
26     virtual ~GrTextContext() {}
27     virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
28                                  GrFontScaler*) = 0;
29
30 protected:
31     GrTextContext(GrContext*, const GrPaint&, const SkPaint&);
32
33     GrPaint                fPaint;
34     SkPaint                fSkPaint;
35     GrContext*             fContext;
36     GrDrawTarget*          fDrawTarget;
37
38     SkIRect                fClipRect;
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() {}
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         static void operator delete(void*, void*) {
76         }
77
78         GrTTextContextManager<TextContextClass>* fManager;
79     };
80
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     }
101
102 private:
103     void recycle(GrTextContext* textContext) {
104         SkASSERT((void*)textContext == fAllocation);
105         SkASSERT(fUsed);
106         fUsed = false;
107     }
108
109     void* fAllocation;
110     bool  fUsed;
111 };
112
113 #endif