blob: f48b8233e91f60711f2ed4b0573af4e3dca1288d [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:
reed@google.comfa35e3d2012-06-26 20:16:17 +000035 SK_DECLARE_INST_COUNT(GrTextContext)
36
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000037 /**
38 * To use a text context it must be wrapped in an AutoFinish. AutoFinish's
39 * destructor ensures all drawing is flushed to the GrContext.
40 */
41 class AutoFinish {
42 public:
43 AutoFinish(GrTextContext* textContext, GrContext* context,
44 const GrPaint&, const GrMatrix* extMatrix);
45 ~AutoFinish();
46 GrTextContext* getTextContext() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000047
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000048 private:
49 GrTextContext* fTextContext;
reed@google.comac10a2d2010-12-22 21:39:39 +000050 };
51
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000052 virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
53 GrFontScaler*) = 0;
bsalomon@google.com5782d712011-01-21 21:03:59 +000054
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000055 virtual ~GrTextContext() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000057protected:
58 GrTextContext() {
59 fContext = NULL;
60 }
61
62 bool isValid() const {
63 return (NULL != fContext);
64 }
65
66 /**
67 * Initialize the object.
68 *
69 * Before call to this method, the instance is considered to be in
70 * invalid state. I.e. call to any method other than isValid will result in
71 * undefined behaviour.
72 *
73 * @see finish
74 */
75 virtual void init(GrContext* context, const GrPaint&,
76 const GrMatrix* extMatrix) {
77 fContext = context;
78 }
79
80 /**
81 * Reset the object to invalid state.
82 *
83 * After call to this method, the instance is considered to be in
84 * invalid state.
85 *
86 * It might be brought back to a valid state by calling init.
87 *
88 * @see init
89 */
90 virtual void finish() {
91 fContext = NULL;
92 }
93
94private:
95 typedef GrRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +000096};
97
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000098inline GrTextContext::AutoFinish::AutoFinish(GrTextContext* textContext,
99 GrContext* context,
100 const GrPaint& grPaint,
101 const GrMatrix* extMatrix) {
102 GrAssert(NULL != textContext);
103 fTextContext = textContext;
104 fTextContext->ref();
105 fTextContext->init(context, grPaint, extMatrix);
106}
107
108inline GrTextContext::AutoFinish::~AutoFinish() {
109 fTextContext->finish();
110 fTextContext->unref();
111}
112
113inline GrTextContext* GrTextContext::AutoFinish::getTextContext() const {
114 return fTextContext;
115}
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117#endif