-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestampRender.cpp
More file actions
136 lines (114 loc) · 3.65 KB
/
TimestampRender.cpp
File metadata and controls
136 lines (114 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#define LOG_NDEBUG 0
#define LOG_TAG "TSRender"
#include <stdint.h>
#include <sys/types.h>
#include <math.h>
#include <fcntl.h>
#include <utils/misc.h>
#include <signal.h>
#include <cutils/properties.h>
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/threads.h>
#include <GLES/egl.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <EGL/eglext.h>
#include <SkString.h>
#include <SkPaint.h>
#include <SkCanvas.h>
#include <SkBitmap.h>
#include <SkStream.h>
#include "TimestampRender.h"
#define DEFAULT_FONT_HEIGHT 60
namespace android {
// ---------------------------------------------------------------------------
TimestampRender::TimestampRender() :
mPaint(NULL), mBitmap(NULL), mCanvas(NULL), mTexture(0),
mFgColor(SK_ColorWHITE), mBgColor(SK_ColorGRAY) {
}
TimestampRender::~TimestampRender() {
glDeleteTextures(1, &mTexture);
mTexture = 0;
if (mPaint) { delete mPaint; mPaint = NULL; }
if (mBitmap) { delete mBitmap; mBitmap = NULL; }
if (mCanvas) { delete mCanvas; mCanvas = NULL; }
}
void TimestampRender::init(int w, int h) {
if (!h) {
h = DEFAULT_FONT_HEIGHT;
}
if (!mPaint) {
mPaint = new SkPaint();
mPaint->setTextSize(SkIntToScalar(h));
mPaint->setColor(mFgColor);
mPaint->setAntiAlias(true);
mPaint->setTextAlign(SkPaint::kCenter_Align);
}
if (!w) {
w = SkScalarRound(mPaint->measureText("888888.8", 8));
}
if (!mBitmap) {
mBitmap = new SkBitmap();
#ifdef FOR_ANDROID_KK
mBitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
mBitmap->allocPixels();
#else
SkImageInfo info = SkImageInfo::Make(w, h, kN32_SkColorType, kPremul_SkAlphaType);
mBitmap->allocPixels(info);
#endif
}
if (!mCanvas) {
mCanvas = new SkCanvas(*mBitmap);
}
if (!mTexture) {
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_2D, mTexture);
GLint crop[4] = { 0, h, w, -h };
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
}
void TimestampRender::setColor(uint32_t fgColor,uint32_t bgColor) {
mBgColor = bgColor;
mFgColor = fgColor;
mPaint->setColor(mFgColor);
}
void TimestampRender::drawText(const char * fmt, ...) {
if (!mBitmap) {
ALOGE("Error on TimestampRender::drawText()! Not initialized!!\n");
return;
}
// build the text
va_list ap;
va_start(ap, fmt);
SkString text;
text.appendVAList(fmt, ap);
va_end(ap);
mBitmap->lockPixels();
// draw background
mCanvas->drawColor(mBgColor);
// draw text, painter is using kCenter_Align
ALOGD("%s %d: DrawText(%s)\n", __FUNCTION__, __LINE__, text.c_str());
mCanvas->drawText(text.c_str(), text.size(), mBitmap->width()/2, mBitmap->height(), *mPaint);
mBitmap->notifyPixelsChanged();
mBitmap->unlockPixels();
}
void TimestampRender::renderToGL(int x, int y) {
if (!mBitmap) {
ALOGE("Error on TimestampRender::render()! Not initialized!!\n");
return;
}
int w = mBitmap->width();
int h = mBitmap->height();
glBindTexture(GL_TEXTURE_2D, mTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mBitmap->getPixels());
glDrawTexiOES(x, y, 0, w, h);
}
// ---------------------------------------------------------------------------
}
; // namespace android