blob: ab1a34453955d0c389debae565c2255b6a7abe8f [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
robertphillips@google.combf5cad42012-05-10 12:40:40 +000025/**
26 * Derived classes can use stages GrPaint::kTotalStages through
27 * GrDrawState::kNumStages-1. The stages before GrPaint::kTotalStages
28 * are reserved for setting up the draw (i.e., textures and filter masks).
29 */
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000030class GrTextContext: public GrRefCnt {
31protected:
reed@google.comac10a2d2010-12-22 21:39:39 +000032 GrContext* fContext;
reed@google.comac10a2d2010-12-22 21:39:39 +000033
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000034public:
35 /**
36 * To use a text context it must be wrapped in an AutoFinish. AutoFinish's
37 * destructor ensures all drawing is flushed to the GrContext.
38 */
39 class AutoFinish {
40 public:
41 AutoFinish(GrTextContext* textContext, GrContext* context,
42 const GrPaint&, const GrMatrix* extMatrix);
43 ~AutoFinish();
44 GrTextContext* getTextContext() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000046 private:
47 GrTextContext* fTextContext;
reed@google.comac10a2d2010-12-22 21:39:39 +000048 };
49
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000050 virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
51 GrFontScaler*) = 0;
bsalomon@google.com5782d712011-01-21 21:03:59 +000052
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000053 virtual ~GrTextContext() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000054
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000055protected:
56 GrTextContext() {
57 fContext = NULL;
58 }
59
60 bool isValid() const {
61 return (NULL != fContext);
62 }
63
64 /**
65 * Initialize the object.
66 *
67 * Before call to this method, the instance is considered to be in
68 * invalid state. I.e. call to any method other than isValid will result in
69 * undefined behaviour.
70 *
71 * @see finish
72 */
73 virtual void init(GrContext* context, const GrPaint&,
74 const GrMatrix* extMatrix) {
75 fContext = context;
76 }
77
78 /**
79 * Reset the object to invalid state.
80 *
81 * After call to this method, the instance is considered to be in
82 * invalid state.
83 *
84 * It might be brought back to a valid state by calling init.
85 *
86 * @see init
87 */
88 virtual void finish() {
89 fContext = NULL;
90 }
91
92private:
93 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000094};
95
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000096inline GrTextContext::AutoFinish::AutoFinish(GrTextContext* textContext,
97 GrContext* context,
98 const GrPaint& grPaint,
99 const GrMatrix* extMatrix) {
100 GrAssert(NULL != textContext);
101 fTextContext = textContext;
102 fTextContext->ref();
103 fTextContext->init(context, grPaint, extMatrix);
104}
105
106inline GrTextContext::AutoFinish::~AutoFinish() {
107 fTextContext->finish();
108 fTextContext->unref();
109}
110
111inline GrTextContext* GrTextContext::AutoFinish::getTextContext() const {
112 return fTextContext;
113}
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115#endif