2012-05-30 16:50:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
2012-05-30 16:50:11 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPicture.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2012-05-30 16:50:11 +00:00
|
|
|
#include "SkPoint.h"
|
2012-07-10 13:17:45 +00:00
|
|
|
#include "SkRandom.h"
|
2012-05-30 16:50:11 +00:00
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class PictureRecordBench : public Benchmark {
|
2012-05-30 16:50:11 +00:00
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
PictureRecordBench(const char name[]) {
|
2012-05-30 16:50:11 +00:00
|
|
|
fName.printf("picture_record_%s", name);
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-30 16:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PICTURE_WIDTH = 1000,
|
|
|
|
PICTURE_HEIGHT = 4000,
|
|
|
|
};
|
|
|
|
protected:
|
2013-09-17 18:58:53 +00:00
|
|
|
virtual const char* onGetName() SK_OVERRIDE {
|
2012-05-30 16:50:11 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
private:
|
2013-09-17 18:58:53 +00:00
|
|
|
SkString fName;
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-30 16:50:11 +00:00
|
|
|
};
|
|
|
|
|
2013-09-17 18:58:53 +00:00
|
|
|
|
|
|
|
static const int kMaxLoopsPerCanvas = 10000;
|
|
|
|
|
2012-05-30 16:50:11 +00:00
|
|
|
/*
|
|
|
|
* An SkPicture has internal dictionaries to store bitmaps, matrices, paints,
|
|
|
|
* and regions. This bench populates those dictionaries to test the speed of
|
|
|
|
* reading and writing to those particular dictionary data structures.
|
|
|
|
*/
|
|
|
|
class DictionaryRecordBench : public PictureRecordBench {
|
|
|
|
public:
|
2013-09-17 18:58:53 +00:00
|
|
|
DictionaryRecordBench() : INHERITED("dictionaries") {}
|
2012-05-30 16:50:11 +00:00
|
|
|
|
|
|
|
protected:
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
2013-09-17 18:58:53 +00:00
|
|
|
SkCanvas* canvas = NULL;
|
2012-05-30 16:50:11 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
const SkPoint translateDelta = getTranslateDelta(loops);
|
2012-05-30 16:50:11 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2013-09-17 18:58:53 +00:00
|
|
|
if (0 == i % kMaxLoopsPerCanvas) {
|
2014-04-13 19:09:42 +00:00
|
|
|
SkAutoTUnref<SkPicture> picture(recorder.endRecording());
|
2014-04-17 23:35:06 +00:00
|
|
|
canvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0);
|
2013-09-17 18:58:53 +00:00
|
|
|
}
|
2012-05-30 16:50:11 +00:00
|
|
|
|
|
|
|
SkColor color = SK_ColorYELLOW + (i % 255);
|
2013-09-10 19:23:38 +00:00
|
|
|
SkIRect rect = SkIRect::MakeWH(i % PICTURE_WIDTH, i % PICTURE_HEIGHT);
|
2012-05-30 16:50:11 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
// set the clip to the given region
|
|
|
|
SkRegion region;
|
|
|
|
region.setRect(rect);
|
|
|
|
canvas->clipRegion(region);
|
|
|
|
|
|
|
|
// fill the clip with a color
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
|
|
|
|
// set a matrix on the canvas
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRotate(SkIntToScalar(i % 360));
|
|
|
|
canvas->setMatrix(matrix);
|
|
|
|
|
|
|
|
// create a simple bitmap
|
|
|
|
SkBitmap bitmap;
|
2014-06-10 02:52:07 +00:00
|
|
|
bitmap.allocPixels(SkImageInfo::Make(10, 10,
|
|
|
|
kRGB_565_SkColorType, kOpaque_SkAlphaType));
|
2012-05-30 16:50:11 +00:00
|
|
|
|
|
|
|
// draw a single color into the bitmap
|
|
|
|
SkCanvas bitmapCanvas(bitmap);
|
|
|
|
bitmapCanvas.drawColor(SkColorSetA(color, i % 255));
|
|
|
|
|
|
|
|
// draw the bitmap onto the canvas
|
|
|
|
canvas->drawBitmapMatrix(bitmap, matrix);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(translateDelta.fX, translateDelta.fY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 19:23:38 +00:00
|
|
|
SkPoint getTranslateDelta(int M) {
|
2012-05-30 16:50:11 +00:00
|
|
|
SkIPoint canvasSize = onGetSize();
|
|
|
|
return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/M),
|
|
|
|
SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/M));
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef PictureRecordBench INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-06-27 20:03:16 +00:00
|
|
|
/*
|
|
|
|
* Populates the SkPaint dictionary with a large number of unique paint
|
|
|
|
* objects that differ only by color
|
|
|
|
*/
|
|
|
|
class UniquePaintDictionaryRecordBench : public PictureRecordBench {
|
|
|
|
public:
|
2013-09-17 18:58:53 +00:00
|
|
|
UniquePaintDictionaryRecordBench() : INHERITED("unique_paint_dictionary") { }
|
2012-06-27 20:03:16 +00:00
|
|
|
|
|
|
|
protected:
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-09-17 17:14:25 +00:00
|
|
|
SkPaint paint;
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
2013-09-17 17:14:25 +00:00
|
|
|
SkCanvas* canvas = NULL;
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2013-09-17 18:58:53 +00:00
|
|
|
if (0 == i % kMaxLoopsPerCanvas) {
|
2014-04-13 19:09:42 +00:00
|
|
|
SkAutoTUnref<SkPicture> picture(recorder.endRecording());
|
2014-04-17 23:35:06 +00:00
|
|
|
canvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0);
|
2013-09-17 17:14:25 +00:00
|
|
|
}
|
2012-07-10 13:17:45 +00:00
|
|
|
paint.setColor(rand.nextU());
|
2012-06-27 20:03:16 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef PictureRecordBench INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Populates the SkPaint dictionary with a number of unique paint
|
2013-07-11 14:26:09 +00:00
|
|
|
* objects that get reused repeatedly.
|
|
|
|
*
|
|
|
|
* Re-creating the paint objects in the inner loop slows the benchmark down 10%.
|
|
|
|
* Using setColor(i % objCount) instead of a random color creates a very high rate
|
|
|
|
* of hash conflicts, slowing us down 12%.
|
2012-06-27 20:03:16 +00:00
|
|
|
*/
|
|
|
|
class RecurringPaintDictionaryRecordBench : public PictureRecordBench {
|
|
|
|
public:
|
2013-09-17 18:58:53 +00:00
|
|
|
RecurringPaintDictionaryRecordBench() : INHERITED("recurring_paint_dictionary") {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-07-11 14:26:09 +00:00
|
|
|
for (int i = 0; i < ObjCount; i++) {
|
|
|
|
fPaint[i].setColor(rand.nextU());
|
|
|
|
}
|
|
|
|
}
|
2012-06-27 20:03:16 +00:00
|
|
|
|
|
|
|
enum {
|
2013-09-10 19:23:38 +00:00
|
|
|
ObjCount = 100, // number of unique paint objects
|
2012-06-27 20:03:16 +00:00
|
|
|
};
|
|
|
|
protected:
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
2014-04-17 23:35:06 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0);
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2013-07-11 14:26:09 +00:00
|
|
|
canvas->drawPaint(fPaint[i % ObjCount]);
|
2012-06-27 20:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-07-11 14:26:09 +00:00
|
|
|
SkPaint fPaint [ObjCount];
|
2012-06-27 20:03:16 +00:00
|
|
|
typedef PictureRecordBench INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-05-30 16:50:11 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new DictionaryRecordBench(); )
|
|
|
|
DEF_BENCH( return new UniquePaintDictionaryRecordBench(); )
|
|
|
|
DEF_BENCH( return new RecurringPaintDictionaryRecordBench(); )
|