2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void call_measure() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
uint16_t text[32];
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-12-03 14:45:17 +00:00
|
|
|
paint.setTextEncoding(kUTF16_SkTextEncoding);
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
|
|
|
|
text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
for (int i = 9; i < 36; i++) {
|
2018-11-12 14:35:15 +00:00
|
|
|
SkFontMetrics m;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(i));
|
|
|
|
paint.getFontMetrics(&m);
|
|
|
|
paint.measureText(text, sizeof(text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void call_draw(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
uint16_t text[32];
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-12-03 14:45:17 +00:00
|
|
|
paint.setTextEncoding(kUTF16_SkTextEncoding);
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int j = 0; j < SK_ARRAY_COUNT(text); j++)
|
|
|
|
text[j] = (uint16_t)((rand.nextU() & 0xFF) + 32);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
SkScalar y = SkIntToScalar(20);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
for (int i = 9; i < 36; i++)
|
|
|
|
{
|
2018-11-12 14:35:15 +00:00
|
|
|
SkFontMetrics m;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(i));
|
|
|
|
paint.getFontMetrics(&m);
|
|
|
|
canvas->drawText(text, sizeof(text), x, y, paint);
|
|
|
|
y += m.fDescent - m.fAscent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool gDone;
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void* measure_proc(void* context) {
|
|
|
|
while (!gDone) {
|
2008-12-17 15:59:43 +00:00
|
|
|
call_measure();
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void* draw_proc(void* context) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap* bm = (SkBitmap*)context;
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
while (!gDone) {
|
2008-12-17 15:59:43 +00:00
|
|
|
call_draw(&canvas);
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class FontCacheView : public Sample {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
enum { N = 4 };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
pthread_t fMThreads[N];
|
|
|
|
pthread_t fDThreads[N];
|
|
|
|
SkBitmap fBitmaps[N];
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
FontCacheView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
gDone = false;
|
2011-05-05 14:03:48 +00:00
|
|
|
for (int i = 0; i < N; i++) {
|
2010-04-22 16:07:49 +00:00
|
|
|
int status;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
status = pthread_create(&fMThreads[i], nullptr, measure_proc, nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(0 == status);
|
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
fBitmaps[i].allocPixels(SkImageInfo::Make(320, 240,
|
|
|
|
kRGB_565_SkColorType,
|
|
|
|
kOpaque_SkAlphaType));
|
2015-08-27 14:41:13 +00:00
|
|
|
status = pthread_create(&fDThreads[i], nullptr, draw_proc, &fBitmaps[i]);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(0 == status);
|
|
|
|
}
|
2011-05-05 14:03:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
virtual ~FontCacheView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
gDone = true;
|
2011-05-05 14:03:48 +00:00
|
|
|
for (int i = 0; i < N; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
void* ret;
|
|
|
|
int status = pthread_join(fMThreads[i], &ret);
|
|
|
|
SkASSERT(0 == status);
|
|
|
|
status = pthread_join(fDThreads[i], &ret);
|
|
|
|
SkASSERT(0 == status);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
virtual bool onQuery(Sample::Event* evt) {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "FontCache");
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
SkScalar y = 0;
|
2011-05-05 14:03:48 +00:00
|
|
|
for (int i = 0; i < N; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawBitmap(fBitmaps[i], x, y);
|
|
|
|
x += SkIntToScalar(fBitmaps[i].width());
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
static Sample* MyFactory() { return new FontCacheView; }
|
|
|
|
static SampleRegister reg(MyFactory);
|