blob: 1539df09a019e5b0227acf7003ee41f6011502ff [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrTextContext_DEFINED
9#define GrTextContext_DEFINED
10
bsalomon@google.com858804d2012-10-15 14:25:50 +000011#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrGlyph.h"
tomhudson@google.com375ff852012-06-29 18:37:57 +000013#include "GrPaint.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000015#include "SkPostConfig.h"
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017class GrContext;
tomhudson@google.com375ff852012-06-29 18:37:57 +000018class GrDrawTarget;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000019class GrFontScaler;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000021/*
22 * This class wraps the state for a single text render
23 */
tomhudson@google.com375ff852012-06-29 18:37:57 +000024class GrTextContext {
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000025public:
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000026 virtual ~GrTextContext() {}
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000027 virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
28 GrFontScaler*) = 0;
29
30protected:
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000031 GrTextContext(GrContext*, const GrPaint&, const SkPaint&);
reed@google.comfa35e3d2012-06-26 20:16:17 +000032
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000033 GrPaint fPaint;
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000034 SkPaint fSkPaint;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000035 GrContext* fContext;
36 GrDrawTarget* fDrawTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +000037
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000038 SkIRect fClipRect;
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000039};
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 */
45class GrTextContextManager {
46public:
47 virtual ~GrTextContextManager() {}
48 virtual GrTextContext* create(GrContext* context, const GrPaint& grPaint,
49 const SkPaint& skPaint) = 0;
50};
51
52template <class TextContextClass>
53class GrTTextContextManager : public GrTextContextManager {
54private:
55 class ManagedTextContext : public TextContextClass {
56 public:
57 ~ManagedTextContext() {}
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +000058
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000059 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 };
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +000080
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000081public:
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 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000101
102private:
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000103 void recycle(GrTextContext* textContext) {
104 SkASSERT((void*)textContext == fAllocation);
105 SkASSERT(fUsed);
106 fUsed = false;
107 }
108
109 void* fAllocation;
110 bool fUsed;
reed@google.comac10a2d2010-12-22 21:39:39 +0000111};
112
113#endif