blob: 588ae6ee34e800f5913234058448b458119eb58d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTextContext_DEFINED
12#define GrTextContext_DEFINED
13
14#include "GrGlyph.h"
bsalomon@google.comcc4dac32011-05-10 13:52:42 +000015#include "GrMatrix.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000016#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
18class GrContext;
reed@google.comac10a2d2010-12-22 21:39:39 +000019class GrFontScaler;
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000020class GrPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000022class SkGpuDevice;
23class SkPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000025class GrTextContext: public GrRefCnt {
26protected:
reed@google.comac10a2d2010-12-22 21:39:39 +000027 GrContext* fContext;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000029public:
30 /**
31 * To use a text context it must be wrapped in an AutoFinish. AutoFinish's
32 * destructor ensures all drawing is flushed to the GrContext.
33 */
34 class AutoFinish {
35 public:
36 AutoFinish(GrTextContext* textContext, GrContext* context,
37 const GrPaint&, const GrMatrix* extMatrix);
38 ~AutoFinish();
39 GrTextContext* getTextContext() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000041 private:
42 GrTextContext* fTextContext;
reed@google.comac10a2d2010-12-22 21:39:39 +000043 };
44
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000045 virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
46 GrFontScaler*) = 0;
bsalomon@google.com5782d712011-01-21 21:03:59 +000047
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000048 virtual ~GrTextContext() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000049
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000050protected:
51 GrTextContext() {
52 fContext = NULL;
53 }
54
55 bool isValid() const {
56 return (NULL != fContext);
57 }
58
59 /**
60 * Initialize the object.
61 *
62 * Before call to this method, the instance is considered to be in
63 * invalid state. I.e. call to any method other than isValid will result in
64 * undefined behaviour.
65 *
66 * @see finish
67 */
68 virtual void init(GrContext* context, const GrPaint&,
69 const GrMatrix* extMatrix) {
70 fContext = context;
71 }
72
73 /**
74 * Reset the object to invalid state.
75 *
76 * After call to this method, the instance is considered to be in
77 * invalid state.
78 *
79 * It might be brought back to a valid state by calling init.
80 *
81 * @see init
82 */
83 virtual void finish() {
84 fContext = NULL;
85 }
86
87private:
88 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000089};
90
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000091inline GrTextContext::AutoFinish::AutoFinish(GrTextContext* textContext,
92 GrContext* context,
93 const GrPaint& grPaint,
94 const GrMatrix* extMatrix) {
95 GrAssert(NULL != textContext);
96 fTextContext = textContext;
97 fTextContext->ref();
98 fTextContext->init(context, grPaint, extMatrix);
99}
100
101inline GrTextContext::AutoFinish::~AutoFinish() {
102 fTextContext->finish();
103 fTextContext->unref();
104}
105
106inline GrTextContext* GrTextContext::AutoFinish::getTextContext() const {
107 return fTextContext;
108}
109
reed@google.comac10a2d2010-12-22 21:39:39 +0000110#endif