aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/gpu/GrTextContext.h
blob: 588ae6ee34e800f5913234058448b458119eb58d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

/*
 * Copyright 2010 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */



#ifndef GrTextContext_DEFINED
#define GrTextContext_DEFINED

#include "GrGlyph.h"
#include "GrMatrix.h"
#include "GrRefCnt.h"

class GrContext;
class GrFontScaler;
class GrPaint;

class SkGpuDevice;
class SkPaint;

class GrTextContext: public GrRefCnt {
protected:
    GrContext*      fContext;

public:
    /**
     * To use a text context it must be wrapped in an AutoFinish. AutoFinish's
     * destructor ensures all drawing is flushed to the GrContext.
     */
    class AutoFinish {
    public:
        AutoFinish(GrTextContext* textContext, GrContext* context,
                   const GrPaint&, const GrMatrix* extMatrix);
        ~AutoFinish();
        GrTextContext* getTextContext() const;

    private:
        GrTextContext* fTextContext;
    };

    virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
                                 GrFontScaler*) = 0;

    virtual ~GrTextContext() {}

protected:
    GrTextContext() {
        fContext = NULL;
    }

    bool isValid() const {
        return (NULL != fContext);
    }

    /**
     * Initialize the object.
     *
     * Before call to this method, the instance is considered to be in
     * invalid state. I.e. call to any method other than isValid will result in
     * undefined behaviour.
     *
     * @see finish
     */
    virtual void init(GrContext* context, const GrPaint&,
                      const GrMatrix* extMatrix) {
        fContext = context;
    }

    /**
     * Reset the object to invalid state.
     *
     * After call to this method, the instance is considered to be in
     * invalid state.
     *
     * It might be brought back to a valid state by calling init.
     *
     * @see init
     */
    virtual void finish() {
        fContext = NULL;
    }

private:
    typedef GrRefCnt INHERITED;
};

inline GrTextContext::AutoFinish::AutoFinish(GrTextContext* textContext,
                                             GrContext* context,
                                             const GrPaint& grPaint,
                                             const GrMatrix* extMatrix) {
    GrAssert(NULL != textContext);
    fTextContext = textContext;
    fTextContext->ref();
    fTextContext->init(context, grPaint, extMatrix);
}

inline GrTextContext::AutoFinish::~AutoFinish() {
    fTextContext->finish();
    fTextContext->unref();
}

inline GrTextContext* GrTextContext::AutoFinish::getTextContext() const {
    return fTextContext;
}

#endif