blob: c572d0d016e264e2eeee34b654972ba6addecb77 [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
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000011#include "GrPoint.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"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000014#include "SkDeviceProperties.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000016#include "SkPostConfig.h"
17
reed@google.comac10a2d2010-12-22 21:39:39 +000018class GrContext;
tomhudson@google.com375ff852012-06-29 18:37:57 +000019class GrDrawTarget;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000020class GrFontScaler;
reed@google.comac10a2d2010-12-22 21:39:39 +000021
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000022/*
23 * This class wraps the state for a single text render
24 */
tomhudson@google.com375ff852012-06-29 18:37:57 +000025class GrTextContext {
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000026public:
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000027 virtual ~GrTextContext() {}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000028 virtual void drawText(const char text[], size_t byteLength, SkScalar x, SkScalar y) = 0;
29 virtual void drawPosText(const char text[], size_t byteLength,
30 const SkScalar pos[], SkScalar constY,
31 int scalarsPerPosition) = 0;
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000032
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000033protected:
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000034 GrTextContext(GrContext*, const GrPaint&, const SkPaint&, const SkDeviceProperties&);
reed@google.comfa35e3d2012-06-26 20:16:17 +000035
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000036 static GrFontScaler* GetGrFontScaler(SkGlyphCache* cache);
37 static void MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
38 const char text[], size_t byteLength, SkVector* stopVector);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000039
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000040 GrContext* fContext;
41 GrPaint fPaint;
42 SkPaint fSkPaint;
43 SkDeviceProperties fDeviceProperties;
44 GrDrawTarget* fDrawTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +000045
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000046 SkIRect fClipRect;
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000047};
48
49/*
50 * These classes wrap the creation of a single text context for a given GPU device. The
51 * assumption is that we'll only be using one text context at a time for that device.
52 */
53class GrTextContextManager {
54public:
55 virtual ~GrTextContextManager() {}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000056 virtual GrTextContext* create(GrContext* grContext, const GrPaint& grPaint,
57 const SkPaint& skPaint, const SkDeviceProperties& props) = 0;
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000058 virtual bool canDraw(const SkPaint& paint, const SkMatrix& ctm) = 0;
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000059};
60
61template <class TextContextClass>
62class GrTTextContextManager : public GrTextContextManager {
63private:
64 class ManagedTextContext : public TextContextClass {
65 public:
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000066 virtual ~ManagedTextContext() {}
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +000067
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000068 ManagedTextContext(GrContext* grContext,
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000069 const GrPaint& grPaint,
70 const SkPaint& skPaint,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000071 const SkDeviceProperties& properties,
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000072 GrTTextContextManager<TextContextClass>* manager) :
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000073 TextContextClass(grContext, grPaint, skPaint, properties) {
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000074 fManager = manager;
75 }
76
77 static void operator delete(void* ptr) {
78 if (ptr == NULL) {
79 return;
80 }
81 ManagedTextContext* context = reinterpret_cast<ManagedTextContext*>(ptr);
82 context->fManager->recycle(context);
83 }
84
85 static void operator delete(void*, void*) {
86 }
87
88 GrTTextContextManager<TextContextClass>* fManager;
89 };
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +000090
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000091public:
92 GrTTextContextManager() {
93 fAllocation = sk_malloc_throw(sizeof(ManagedTextContext));
94 fUsed = false;
95 }
96
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000097 virtual ~GrTTextContextManager() {
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000098 SkASSERT(!fUsed);
99 sk_free(fAllocation);
100 }
101
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000102 virtual GrTextContext* create(GrContext* grContext, const GrPaint& grPaint,
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000103 const SkPaint& skPaint, const SkDeviceProperties& properties)
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000104 SK_OVERRIDE {
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000105 // add check for usePath here?
106 SkASSERT(!fUsed);
107 ManagedTextContext* obj = SkNEW_PLACEMENT_ARGS(fAllocation, ManagedTextContext,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000108 (grContext, grPaint, skPaint, properties,
109 this));
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000110 fUsed = true;
111 return obj;
112 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000113
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +0000114 virtual bool canDraw(const SkPaint& paint, const SkMatrix& ctm) SK_OVERRIDE {
115 return TextContextClass::CanDraw(paint, ctm);
116 }
117
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000118private:
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000119 void recycle(GrTextContext* textContext) {
120 SkASSERT((void*)textContext == fAllocation);
121 SkASSERT(fUsed);
122 fUsed = false;
123 }
124
125 void* fAllocation;
126 bool fUsed;
reed@google.comac10a2d2010-12-22 21:39:39 +0000127};
128
129#endif