blob: a8c0df0bda7c412018e185b9204809a91c953d99 [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;
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000032
33protected:
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);
39
40 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.orgcc40f062014-01-24 14:38:27 +000058};
59
60template <class TextContextClass>
61class GrTTextContextManager : public GrTextContextManager {
62private:
63 class ManagedTextContext : public TextContextClass {
64 public:
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000065 virtual ~ManagedTextContext() {}
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +000066
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000067 ManagedTextContext(GrContext* grContext,
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000068 const GrPaint& grPaint,
69 const SkPaint& skPaint,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000070 const SkDeviceProperties& properties,
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000071 GrTTextContextManager<TextContextClass>* manager) :
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000072 TextContextClass(grContext, grPaint, skPaint, properties) {
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000073 fManager = manager;
74 }
75
76 static void operator delete(void* ptr) {
77 if (ptr == NULL) {
78 return;
79 }
80 ManagedTextContext* context = reinterpret_cast<ManagedTextContext*>(ptr);
81 context->fManager->recycle(context);
82 }
83
84 static void operator delete(void*, void*) {
85 }
86
87 GrTTextContextManager<TextContextClass>* fManager;
88 };
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +000089
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000090public:
91 GrTTextContextManager() {
92 fAllocation = sk_malloc_throw(sizeof(ManagedTextContext));
93 fUsed = false;
94 }
95
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000096 virtual ~GrTTextContextManager() {
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000097 SkASSERT(!fUsed);
98 sk_free(fAllocation);
99 }
100
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000101 virtual GrTextContext* create(GrContext* grContext, const GrPaint& grPaint,
102 const SkPaint& skPaint, const SkDeviceProperties& properties)
103 SK_OVERRIDE {
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000104 // add check for usePath here?
105 SkASSERT(!fUsed);
106 ManagedTextContext* obj = SkNEW_PLACEMENT_ARGS(fAllocation, ManagedTextContext,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000107 (grContext, grPaint, skPaint, properties,
108 this));
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000109 fUsed = true;
110 return obj;
111 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000112
113private:
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000114 void recycle(GrTextContext* textContext) {
115 SkASSERT((void*)textContext == fAllocation);
116 SkASSERT(fUsed);
117 fUsed = false;
118 }
119
120 void* fAllocation;
121 bool fUsed;
reed@google.comac10a2d2010-12-22 21:39:39 +0000122};
123
124#endif