2012-08-20 21:02:49 +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.
|
|
|
|
*/
|
|
|
|
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "PictureRenderer.h"
|
2012-08-31 16:15:22 +00:00
|
|
|
#include "picture_utils.h"
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "SamplePipeControllers.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkGPipe.h"
|
2012-08-31 16:15:22 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#endif
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageEncoder.h"
|
2012-11-06 21:26:13 +00:00
|
|
|
#include "SkMaskFilter.h"
|
2012-08-23 15:24:13 +00:00
|
|
|
#include "SkMatrix.h"
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "SkPicture.h"
|
2012-11-02 18:11:49 +00:00
|
|
|
#include "SkRTree.h"
|
2012-08-23 15:24:13 +00:00
|
|
|
#include "SkScalar.h"
|
2012-11-07 17:52:48 +00:00
|
|
|
#include "SkStream.h"
|
2012-08-21 19:05:08 +00:00
|
|
|
#include "SkString.h"
|
2012-08-31 16:15:22 +00:00
|
|
|
#include "SkTemplates.h"
|
2012-12-13 16:39:53 +00:00
|
|
|
#include "SkTileGridPicture.h"
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "SkTDArray.h"
|
2012-08-31 16:15:22 +00:00
|
|
|
#include "SkThreadUtils.h"
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "SkTypes.h"
|
2012-11-29 21:00:39 +00:00
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkPictureUtils.h"
|
2012-08-20 15:03:29 +00:00
|
|
|
|
2012-07-26 17:27:57 +00:00
|
|
|
namespace sk_tools {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kDefaultTileWidth = 256,
|
|
|
|
kDefaultTileHeight = 256
|
|
|
|
};
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void PictureRenderer::init(SkPicture* pict) {
|
2012-08-20 15:03:44 +00:00
|
|
|
SkASSERT(NULL == fPicture);
|
|
|
|
SkASSERT(NULL == fCanvas.get());
|
|
|
|
if (fPicture != NULL || NULL != fCanvas.get()) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(pict != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
if (NULL == pict) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fPicture = pict;
|
2012-11-02 18:11:49 +00:00
|
|
|
fPicture->ref();
|
2012-08-20 15:03:57 +00:00
|
|
|
fCanvas.reset(this->setupCanvas());
|
|
|
|
}
|
|
|
|
|
2012-11-06 21:26:13 +00:00
|
|
|
class FlagsDrawFilter : public SkDrawFilter {
|
|
|
|
public:
|
|
|
|
FlagsDrawFilter(PictureRenderer::DrawFilterFlags* flags) :
|
|
|
|
fFlags(flags) {}
|
|
|
|
|
2012-11-26 20:26:54 +00:00
|
|
|
virtual bool filter(SkPaint* paint, Type t) {
|
2012-11-06 21:26:13 +00:00
|
|
|
paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags);
|
2012-12-18 18:20:44 +00:00
|
|
|
if (PictureRenderer::kBlur_DrawFilterFlag & fFlags[t]) {
|
2012-11-06 21:26:13 +00:00
|
|
|
SkMaskFilter* maskFilter = paint->getMaskFilter();
|
|
|
|
SkMaskFilter::BlurInfo blurInfo;
|
|
|
|
if (maskFilter && maskFilter->asABlur(&blurInfo)) {
|
2012-12-18 18:20:44 +00:00
|
|
|
paint->setMaskFilter(NULL);
|
2012-11-06 21:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) {
|
|
|
|
paint->setHinting(SkPaint::kNo_Hinting);
|
|
|
|
} else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) {
|
|
|
|
paint->setHinting(SkPaint::kSlight_Hinting);
|
|
|
|
}
|
2012-11-26 20:26:54 +00:00
|
|
|
return true;
|
2012-11-06 21:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PictureRenderer::DrawFilterFlags* fFlags;
|
|
|
|
};
|
|
|
|
|
2012-12-17 19:25:54 +00:00
|
|
|
static void setUpFilter(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* drawFilters) {
|
2012-11-06 21:26:13 +00:00
|
|
|
if (drawFilters && !canvas->getDrawFilter()) {
|
|
|
|
canvas->setDrawFilter(SkNEW_ARGS(FlagsDrawFilter, (drawFilters)))->unref();
|
2012-11-07 16:42:17 +00:00
|
|
|
if (drawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) {
|
|
|
|
canvas->setAllowSoftClip(false);
|
|
|
|
}
|
2012-11-06 21:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 15:03:57 +00:00
|
|
|
SkCanvas* PictureRenderer::setupCanvas() {
|
2012-12-13 21:40:48 +00:00
|
|
|
const int width = this->getViewWidth();
|
|
|
|
const int height = this->getViewHeight();
|
|
|
|
return this->setupCanvas(width, height);
|
2012-08-20 15:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* PictureRenderer::setupCanvas(int width, int height) {
|
2012-11-06 21:26:13 +00:00
|
|
|
SkCanvas* canvas;
|
2012-08-20 15:03:29 +00:00
|
|
|
switch(fDeviceType) {
|
|
|
|
case kBitmap_DeviceType: {
|
|
|
|
SkBitmap bitmap;
|
2012-08-20 15:03:57 +00:00
|
|
|
sk_tools::setup_bitmap(&bitmap, width, height);
|
2012-11-06 21:26:13 +00:00
|
|
|
canvas = SkNEW_ARGS(SkCanvas, (bitmap));
|
2012-08-20 15:03:29 +00:00
|
|
|
}
|
2012-12-17 19:25:54 +00:00
|
|
|
break;
|
2012-08-20 15:03:29 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
case kGPU_DeviceType: {
|
|
|
|
SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice,
|
2012-08-20 15:03:33 +00:00
|
|
|
(fGrContext, SkBitmap::kARGB_8888_Config,
|
2012-08-20 15:03:57 +00:00
|
|
|
width, height)));
|
2012-11-06 21:26:13 +00:00
|
|
|
canvas = SkNEW_ARGS(SkCanvas, (device.get()));
|
2012-08-20 15:03:29 +00:00
|
|
|
}
|
2012-12-17 19:25:54 +00:00
|
|
|
break;
|
2012-08-20 15:03:29 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
2012-12-17 19:25:54 +00:00
|
|
|
return NULL;
|
2012-08-20 15:03:29 +00:00
|
|
|
}
|
2012-12-17 19:25:54 +00:00
|
|
|
setUpFilter(canvas, fDrawFilters);
|
|
|
|
this->scaleToScaleFactor(canvas);
|
|
|
|
return canvas;
|
|
|
|
}
|
2012-08-20 15:03:57 +00:00
|
|
|
|
2012-12-17 19:25:54 +00:00
|
|
|
void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) {
|
|
|
|
SkASSERT(canvas != NULL);
|
|
|
|
if (fScaleFactor != SK_Scalar1) {
|
|
|
|
canvas->scale(fScaleFactor, fScaleFactor);
|
|
|
|
}
|
2012-08-07 17:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PictureRenderer::end() {
|
2012-08-20 15:03:47 +00:00
|
|
|
this->resetState();
|
2012-11-02 18:11:49 +00:00
|
|
|
SkSafeUnref(fPicture);
|
2012-08-07 17:11:33 +00:00
|
|
|
fPicture = NULL;
|
|
|
|
fCanvas.reset(NULL);
|
|
|
|
}
|
|
|
|
|
2012-12-13 21:40:48 +00:00
|
|
|
int PictureRenderer::getViewWidth() {
|
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
int width = fPicture->width();
|
|
|
|
if (fViewport.width() > 0) {
|
|
|
|
width = SkMin32(width, fViewport.width());
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PictureRenderer::getViewHeight() {
|
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
int height = fPicture->height();
|
|
|
|
if (fViewport.height() > 0) {
|
|
|
|
height = SkMin32(height, fViewport.height());
|
|
|
|
}
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:11:49 +00:00
|
|
|
/** Converts fPicture to a picture that uses a BBoxHierarchy.
|
|
|
|
* PictureRenderer subclasses that are used to test picture playback
|
|
|
|
* should call this method during init.
|
|
|
|
*/
|
|
|
|
void PictureRenderer::buildBBoxHierarchy() {
|
|
|
|
SkASSERT(NULL != fPicture);
|
|
|
|
if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) {
|
|
|
|
SkPicture* newPicture = this->createPicture();
|
|
|
|
SkCanvas* recorder = newPicture->beginRecording(fPicture->width(), fPicture->height(),
|
|
|
|
this->recordFlags());
|
|
|
|
fPicture->draw(recorder);
|
|
|
|
newPicture->endRecording();
|
|
|
|
fPicture->unref();
|
|
|
|
fPicture = newPicture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 15:03:47 +00:00
|
|
|
void PictureRenderer::resetState() {
|
2012-08-20 15:04:12 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2012-08-20 15:03:47 +00:00
|
|
|
if (this->isUsingGpuDevice()) {
|
|
|
|
SkGLContext* glContext = fGrContextFactory.getGLContext(
|
|
|
|
GrContextFactory::kNative_GLContextType);
|
2012-08-20 15:04:15 +00:00
|
|
|
|
|
|
|
SkASSERT(glContext != NULL);
|
|
|
|
if (NULL == glContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-07 15:21:18 +00:00
|
|
|
fGrContext->flush();
|
2012-08-20 15:03:47 +00:00
|
|
|
SK_GL(*glContext, Finish());
|
|
|
|
}
|
2012-08-20 15:04:12 +00:00
|
|
|
#endif
|
2012-08-20 15:03:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 18:11:49 +00:00
|
|
|
uint32_t PictureRenderer::recordFlags() {
|
2013-01-16 20:12:22 +00:00
|
|
|
return ((kNone_BBoxHierarchyType == fBBoxHierarchyType) ? 0 :
|
|
|
|
SkPicture::kOptimizeForClippedPlayback_RecordingFlag) |
|
|
|
|
SkPicture::kUsePathBoundsForClip_RecordingFlag;
|
2012-11-02 18:11:49 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 17:32:33 +00:00
|
|
|
/**
|
|
|
|
* Write the canvas to the specified path.
|
|
|
|
* @param canvas Must be non-null. Canvas to be written to a file.
|
|
|
|
* @param path Path for the file to be written. Should have no extension; write() will append
|
|
|
|
* an appropriate one. Passed in by value so it can be modified.
|
|
|
|
* @return bool True if the Canvas is written to a file.
|
|
|
|
*/
|
|
|
|
static bool write(SkCanvas* canvas, SkString path) {
|
2012-09-20 14:54:21 +00:00
|
|
|
SkASSERT(canvas != NULL);
|
2012-10-03 17:32:33 +00:00
|
|
|
if (NULL == canvas) {
|
2012-08-21 19:05:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
2012-09-20 14:54:21 +00:00
|
|
|
SkISize size = canvas->getDeviceSize();
|
|
|
|
sk_tools::setup_bitmap(&bitmap, size.width(), size.height());
|
2012-08-21 19:05:08 +00:00
|
|
|
|
2012-09-20 14:54:21 +00:00
|
|
|
canvas->readPixels(&bitmap, 0, 0);
|
2012-08-21 19:05:08 +00:00
|
|
|
sk_tools::force_all_opaque(bitmap);
|
|
|
|
|
2012-09-20 14:54:21 +00:00
|
|
|
// Since path is passed in by value, it is okay to modify it.
|
|
|
|
path.append(".png");
|
2012-08-21 19:05:08 +00:00
|
|
|
return SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
|
|
|
|
}
|
|
|
|
|
2012-10-03 17:32:33 +00:00
|
|
|
/**
|
|
|
|
* If path is non NULL, append number to it, and call write(SkCanvas*, SkString) to write the
|
|
|
|
* provided canvas to a file. Returns true if path is NULL or if write() succeeds.
|
|
|
|
*/
|
|
|
|
static bool writeAppendNumber(SkCanvas* canvas, const SkString* path, int number) {
|
|
|
|
if (NULL == path) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkString pathWithNumber(*path);
|
|
|
|
pathWithNumber.appendf("%i", number);
|
|
|
|
return write(canvas, pathWithNumber);
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-06 16:54:40 +00:00
|
|
|
SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) {
|
|
|
|
// defer the canvas setup until the render step
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:52:48 +00:00
|
|
|
static bool PNGEncodeBitmapToStream(SkWStream* wStream, const SkBitmap& bm) {
|
|
|
|
return SkImageEncoder::EncodeStream(wStream, bm, SkImageEncoder::kPNG_Type, 100);
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool RecordPictureRenderer::render(const SkString* path, SkBitmap** out) {
|
2012-11-02 18:11:49 +00:00
|
|
|
SkAutoTUnref<SkPicture> replayer(this->createPicture());
|
2012-12-17 19:25:54 +00:00
|
|
|
SkCanvas* recorder = replayer->beginRecording(this->getViewWidth(), this->getViewHeight(),
|
2012-11-02 18:11:49 +00:00
|
|
|
this->recordFlags());
|
2012-12-17 19:25:54 +00:00
|
|
|
this->scaleToScaleFactor(recorder);
|
2012-09-07 15:21:18 +00:00
|
|
|
fPicture->draw(recorder);
|
2012-11-02 18:11:49 +00:00
|
|
|
replayer->endRecording();
|
2012-11-07 17:52:48 +00:00
|
|
|
if (path != NULL) {
|
|
|
|
// Record the new picture as a new SKP with PNG encoded bitmaps.
|
|
|
|
SkString skpPath(*path);
|
|
|
|
// ".skp" was removed from 'path' before being passed in here.
|
|
|
|
skpPath.append(".skp");
|
|
|
|
SkFILEWStream stream(skpPath.c_str());
|
|
|
|
replayer->serialize(&stream, &PNGEncodeBitmapToStream);
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-20 14:54:21 +00:00
|
|
|
return false;
|
2012-09-07 15:21:18 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:01:26 +00:00
|
|
|
SkString RecordPictureRenderer::getConfigNameInternal() {
|
|
|
|
return SkString("record");
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool PipePictureRenderer::render(const SkString* path, SkBitmap** out) {
|
2012-08-07 17:11:33 +00:00
|
|
|
SkASSERT(fCanvas.get() != NULL);
|
|
|
|
SkASSERT(fPicture != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
if (NULL == fCanvas.get() || NULL == fPicture) {
|
2012-09-20 14:54:21 +00:00
|
|
|
return false;
|
2012-08-07 17:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PipeController pipeController(fCanvas.get());
|
2012-07-26 17:27:57 +00:00
|
|
|
SkGPipeWriter writer;
|
|
|
|
SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
|
2012-08-07 17:11:33 +00:00
|
|
|
pipeCanvas->drawPicture(*fPicture);
|
2012-07-26 17:27:57 +00:00
|
|
|
writer.endRecording();
|
2012-09-07 15:21:18 +00:00
|
|
|
fCanvas->flush();
|
2012-10-26 13:26:55 +00:00
|
|
|
if (NULL != path) {
|
|
|
|
return write(fCanvas, *path);
|
|
|
|
}
|
2012-12-18 22:24:03 +00:00
|
|
|
if (NULL != out) {
|
|
|
|
*out = SkNEW(SkBitmap);
|
|
|
|
setup_bitmap(*out, fPicture->width(), fPicture->height());
|
|
|
|
fCanvas->readPixels(*out, 0, 0);
|
2012-12-19 02:01:38 +00:00
|
|
|
}
|
2012-10-26 13:26:55 +00:00
|
|
|
return true;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:01:26 +00:00
|
|
|
SkString PipePictureRenderer::getConfigNameInternal() {
|
|
|
|
return SkString("pipe");
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-02 18:11:49 +00:00
|
|
|
void SimplePictureRenderer::init(SkPicture* picture) {
|
|
|
|
INHERITED::init(picture);
|
|
|
|
this->buildBBoxHierarchy();
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool SimplePictureRenderer::render(const SkString* path, SkBitmap** out) {
|
2012-08-07 17:11:33 +00:00
|
|
|
SkASSERT(fCanvas.get() != NULL);
|
|
|
|
SkASSERT(fPicture != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
if (NULL == fCanvas.get() || NULL == fPicture) {
|
2012-09-20 14:54:21 +00:00
|
|
|
return false;
|
2012-08-07 17:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fCanvas->drawPicture(*fPicture);
|
2012-09-07 15:21:18 +00:00
|
|
|
fCanvas->flush();
|
2012-10-26 13:26:55 +00:00
|
|
|
if (NULL != path) {
|
|
|
|
return write(fCanvas, *path);
|
|
|
|
}
|
2012-12-19 02:01:38 +00:00
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
if (NULL != out) {
|
|
|
|
*out = SkNEW(SkBitmap);
|
|
|
|
setup_bitmap(*out, fPicture->width(), fPicture->height());
|
|
|
|
fCanvas->readPixels(*out, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-10-26 13:26:55 +00:00
|
|
|
return true;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:01:26 +00:00
|
|
|
SkString SimplePictureRenderer::getConfigNameInternal() {
|
|
|
|
return SkString("simple");
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-26 17:27:57 +00:00
|
|
|
TiledPictureRenderer::TiledPictureRenderer()
|
2012-11-02 21:28:12 +00:00
|
|
|
: fTileWidth(kDefaultTileWidth)
|
2012-08-29 17:35:07 +00:00
|
|
|
, fTileHeight(kDefaultTileHeight)
|
2012-09-10 19:01:38 +00:00
|
|
|
, fTileWidthPercentage(0.0)
|
2012-08-29 17:35:07 +00:00
|
|
|
, fTileHeightPercentage(0.0)
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
, fTileMinPowerOf2Width(0)
|
|
|
|
, fCurrentTileOffset(-1)
|
|
|
|
, fTilesX(0)
|
|
|
|
, fTilesY(0) { }
|
2012-07-26 17:27:57 +00:00
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::init(SkPicture* pict) {
|
|
|
|
SkASSERT(pict != NULL);
|
2012-09-18 14:32:35 +00:00
|
|
|
SkASSERT(0 == fTileRects.count());
|
|
|
|
if (NULL == pict || fTileRects.count() != 0) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
// Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
|
|
|
|
// used by bench_pictures.
|
|
|
|
fPicture = pict;
|
2012-11-02 18:11:49 +00:00
|
|
|
fPicture->ref();
|
2012-11-02 21:28:12 +00:00
|
|
|
this->buildBBoxHierarchy();
|
2012-07-27 20:09:26 +00:00
|
|
|
|
|
|
|
if (fTileWidthPercentage > 0) {
|
2012-08-15 14:36:41 +00:00
|
|
|
fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100));
|
2012-07-27 20:09:26 +00:00
|
|
|
}
|
|
|
|
if (fTileHeightPercentage > 0) {
|
2012-08-15 14:36:41 +00:00
|
|
|
fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100));
|
2012-07-27 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 20:53:25 +00:00
|
|
|
if (fTileMinPowerOf2Width > 0) {
|
|
|
|
this->setupPowerOf2Tiles();
|
|
|
|
} else {
|
|
|
|
this->setupTiles();
|
|
|
|
}
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight));
|
|
|
|
// Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the
|
|
|
|
// first call to drawCurrentTile.
|
|
|
|
fCurrentTileOffset = -1;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::end() {
|
2012-09-20 14:42:33 +00:00
|
|
|
fTileRects.reset();
|
2012-08-07 17:11:33 +00:00
|
|
|
this->INHERITED::end();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::setupTiles() {
|
2012-12-13 21:40:48 +00:00
|
|
|
// Only use enough tiles to cover the viewport
|
|
|
|
const int width = this->getViewWidth();
|
|
|
|
const int height = this->getViewHeight();
|
|
|
|
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
fTilesX = fTilesY = 0;
|
2012-12-13 21:40:48 +00:00
|
|
|
for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
fTilesY++;
|
2012-12-13 21:40:48 +00:00
|
|
|
for (int tile_x_start = 0; tile_x_start < width; tile_x_start += fTileWidth) {
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
if (0 == tile_y_start) {
|
|
|
|
// Only count tiles in the X direction on the first pass.
|
|
|
|
fTilesX++;
|
|
|
|
}
|
2012-09-18 14:32:35 +00:00
|
|
|
*fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
|
|
|
|
SkIntToScalar(tile_y_start),
|
|
|
|
SkIntToScalar(fTileWidth),
|
|
|
|
SkIntToScalar(fTileHeight));
|
2012-08-23 20:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
bool TiledPictureRenderer::tileDimensions(int &x, int &y) {
|
|
|
|
if (fTileRects.count() == 0 || NULL == fPicture) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
x = fTilesX;
|
|
|
|
y = fTilesY;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 20:53:25 +00:00
|
|
|
// The goal of the powers of two tiles is to minimize the amount of wasted tile
|
|
|
|
// space in the width-wise direction and then minimize the number of tiles. The
|
|
|
|
// constraints are that every tile must have a pixel width that is a power of
|
|
|
|
// two and also be of some minimal width (that is also a power of two).
|
|
|
|
//
|
2012-08-31 16:15:22 +00:00
|
|
|
// This is solved by first taking our picture size and rounding it up to the
|
2012-08-23 20:53:25 +00:00
|
|
|
// multiple of the minimal width. The binary representation of this rounded
|
|
|
|
// value gives us the tiles we need: a bit of value one means we need a tile of
|
|
|
|
// that size.
|
|
|
|
void TiledPictureRenderer::setupPowerOf2Tiles() {
|
2012-12-13 21:40:48 +00:00
|
|
|
// Only use enough tiles to cover the viewport
|
|
|
|
const int width = this->getViewWidth();
|
|
|
|
const int height = this->getViewHeight();
|
|
|
|
|
|
|
|
int rounded_value = width;
|
|
|
|
if (width % fTileMinPowerOf2Width != 0) {
|
|
|
|
rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width;
|
2012-08-23 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 21:40:48 +00:00
|
|
|
int num_bits = SkScalarCeilToInt(SkScalarLog2(SkIntToScalar(width)));
|
2012-08-23 20:53:25 +00:00
|
|
|
int largest_possible_tile_size = 1 << num_bits;
|
|
|
|
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
fTilesX = fTilesY = 0;
|
2012-08-23 20:53:25 +00:00
|
|
|
// The tile height is constant for a particular picture.
|
2012-12-13 21:40:48 +00:00
|
|
|
for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
fTilesY++;
|
2012-08-23 20:53:25 +00:00
|
|
|
int tile_x_start = 0;
|
|
|
|
int current_width = largest_possible_tile_size;
|
2012-09-18 14:32:35 +00:00
|
|
|
// Set fTileWidth to be the width of the widest tile, so that each canvas is large enough
|
|
|
|
// to draw each tile.
|
|
|
|
fTileWidth = current_width;
|
2012-08-23 20:53:25 +00:00
|
|
|
|
|
|
|
while (current_width >= fTileMinPowerOf2Width) {
|
|
|
|
// It is very important this is a bitwise AND.
|
|
|
|
if (current_width & rounded_value) {
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
if (0 == tile_y_start) {
|
|
|
|
// Only count tiles in the X direction on the first pass.
|
|
|
|
fTilesX++;
|
|
|
|
}
|
2012-09-18 14:32:35 +00:00
|
|
|
*fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
|
|
|
|
SkIntToScalar(tile_y_start),
|
|
|
|
SkIntToScalar(current_width),
|
|
|
|
SkIntToScalar(fTileHeight));
|
2012-08-23 20:53:25 +00:00
|
|
|
tile_x_start += current_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_width >>= 1;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
/**
|
|
|
|
* Draw the specified playback to the canvas translated to rectangle provided, so that this mini
|
|
|
|
* canvas represents the rectangle's portion of the overall picture.
|
|
|
|
* Saves and restores so that the initial clip and matrix return to their state before this function
|
|
|
|
* is called.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
static void DrawTileToCanvas(SkCanvas* canvas, const SkRect& tileRect, T* playback) {
|
|
|
|
int saveCount = canvas->save();
|
2012-12-17 19:25:54 +00:00
|
|
|
// Translate so that we draw the correct portion of the picture.
|
|
|
|
// Perform a postTranslate so that the scaleFactor does not interfere with the positioning.
|
|
|
|
SkMatrix mat(canvas->getTotalMatrix());
|
|
|
|
mat.postTranslate(-tileRect.fLeft, -tileRect.fTop);
|
|
|
|
canvas->setMatrix(mat);
|
2012-09-20 14:42:33 +00:00
|
|
|
playback->draw(canvas);
|
|
|
|
canvas->restoreToCount(saveCount);
|
|
|
|
canvas->flush();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
static void bitmapCopySubset(const SkBitmap& src, SkBitmap* dst, int xDst,
|
|
|
|
int yDst) {
|
|
|
|
for (int y = 0; y <src.height() && y + yDst < dst->height() ; y++) {
|
|
|
|
for (int x = 0; x < src.width() && x + xDst < dst->width() ; x++) {
|
|
|
|
*dst->getAddr32(xDst + x, yDst + y) = *src.getAddr32(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
bool TiledPictureRenderer::nextTile(int &i, int &j) {
|
|
|
|
if (++fCurrentTileOffset < fTileRects.count()) {
|
|
|
|
i = fCurrentTileOffset % fTilesX;
|
|
|
|
j = fCurrentTileOffset / fTilesX;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TiledPictureRenderer::drawCurrentTile() {
|
|
|
|
SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count());
|
|
|
|
DrawTileToCanvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture);
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool TiledPictureRenderer::render(const SkString* path, SkBitmap** out) {
|
2012-11-02 21:28:12 +00:00
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
if (NULL == fPicture) {
|
|
|
|
return false;
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
if (out){
|
|
|
|
*out = SkNEW(SkBitmap);
|
|
|
|
setup_bitmap(*out, fPicture->width(), fPicture->height());
|
|
|
|
setup_bitmap(&bitmap, fTileWidth, fTileHeight);
|
|
|
|
}
|
2012-11-02 21:28:12 +00:00
|
|
|
bool success = true;
|
|
|
|
for (int i = 0; i < fTileRects.count(); ++i) {
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
DrawTileToCanvas(fCanvas, fTileRects[i], fPicture);
|
2012-11-02 21:28:12 +00:00
|
|
|
if (NULL != path) {
|
Provide an option to bench drawing individual tiles in bench_pictures.
Provides output like the following:
running bench [1236 12045] androidpolice.skp
tile_256x256: tile [0,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [1,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [2,0] out of [5,48]: msecs = 1.00
tile_256x256: tile [3,0] out of [5,48]: msecs = 1.50
tile_256x256: tile [4,0] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,1] out of [5,48]: msecs = 2.00
tile_256x256: tile [1,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [2,1] out of [5,48]: msecs = 3.50
tile_256x256: tile [3,1] out of [5,48]: msecs = 6.00
tile_256x256: tile [4,1] out of [5,48]: msecs = 2.50
tile_256x256: tile [0,2] out of [5,48]: msecs = 2.00
BUG=https://code.google.com/p/skia/issues/detail?id=1016
Review URL: https://codereview.appspot.com/6937047
git-svn-id: http://skia.googlecode.com/svn/trunk@6805 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-12-13 22:09:28 +00:00
|
|
|
success &= writeAppendNumber(fCanvas, path, i);
|
2012-10-03 17:32:33 +00:00
|
|
|
}
|
2012-12-18 22:24:03 +00:00
|
|
|
if (NULL != out) {
|
|
|
|
if (fCanvas->readPixels(&bitmap, 0, 0)) {
|
2013-01-07 18:41:28 +00:00
|
|
|
bitmapCopySubset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()),
|
|
|
|
SkScalarFloorToInt(fTileRects[i].top()));
|
2012-12-18 22:24:03 +00:00
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
2012-11-02 21:28:12 +00:00
|
|
|
return success;
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
2012-08-31 16:15:22 +00:00
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
|
|
|
|
SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
|
|
|
|
SkASSERT(fPicture != NULL);
|
2012-12-17 19:25:54 +00:00
|
|
|
// Clip the tile to an area that is completely inside both the SkPicture and the viewport. This
|
|
|
|
// is mostly important for tiles on the right and bottom edges as they may go over this area and
|
|
|
|
// the picture may have some commands that draw outside of this area and so should not actually
|
|
|
|
// be written.
|
|
|
|
// Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set
|
|
|
|
// by INHERITED::setupCanvas.
|
|
|
|
SkRegion clipRegion;
|
|
|
|
clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight());
|
|
|
|
canvas->clipRegion(clipRegion);
|
2012-11-02 21:28:12 +00:00
|
|
|
return canvas;
|
|
|
|
}
|
2012-11-02 22:01:26 +00:00
|
|
|
|
|
|
|
SkString TiledPictureRenderer::getConfigNameInternal() {
|
|
|
|
SkString name;
|
|
|
|
if (fTileMinPowerOf2Width > 0) {
|
|
|
|
name.append("pow2tile_");
|
|
|
|
name.appendf("%i", fTileMinPowerOf2Width);
|
|
|
|
} else {
|
|
|
|
name.append("tile_");
|
|
|
|
if (fTileWidthPercentage > 0) {
|
|
|
|
name.appendf("%.f%%", fTileWidthPercentage);
|
|
|
|
} else {
|
|
|
|
name.appendf("%i", fTileWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name.append("x");
|
|
|
|
if (fTileHeightPercentage > 0) {
|
|
|
|
name.appendf("%.f%%", fTileHeightPercentage);
|
|
|
|
} else {
|
|
|
|
name.appendf("%i", fTileHeight);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2012-08-31 16:15:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
// Holds all of the information needed to draw a set of tiles.
|
|
|
|
class CloneData : public SkRunnable {
|
|
|
|
|
|
|
|
public:
|
|
|
|
CloneData(SkPicture* clone, SkCanvas* canvas, SkTDArray<SkRect>& rects, int start, int end,
|
|
|
|
SkRunnable* done)
|
|
|
|
: fClone(clone)
|
|
|
|
, fCanvas(canvas)
|
|
|
|
, fPath(NULL)
|
|
|
|
, fRects(rects)
|
|
|
|
, fStart(start)
|
|
|
|
, fEnd(end)
|
|
|
|
, fSuccess(NULL)
|
|
|
|
, fDone(done) {
|
|
|
|
SkASSERT(fDone != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run() SK_OVERRIDE {
|
|
|
|
SkGraphics::SetTLSFontCacheLimit(1024 * 1024);
|
2012-12-18 22:24:03 +00:00
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
if (fBitmap != NULL) {
|
|
|
|
// All tiles are the same size.
|
2013-01-07 18:41:28 +00:00
|
|
|
setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScalarFloorToInt(fRects[0].height()));
|
2012-12-18 22:24:03 +00:00
|
|
|
}
|
2012-12-19 02:01:38 +00:00
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
for (int i = fStart; i < fEnd; i++) {
|
|
|
|
DrawTileToCanvas(fCanvas, fRects[i], fClone);
|
|
|
|
if (fPath != NULL && !writeAppendNumber(fCanvas, fPath, i)
|
|
|
|
&& fSuccess != NULL) {
|
|
|
|
*fSuccess = false;
|
|
|
|
// If one tile fails to write to a file, do not continue drawing the rest.
|
|
|
|
break;
|
|
|
|
}
|
2012-12-18 22:24:03 +00:00
|
|
|
if (fBitmap != NULL) {
|
|
|
|
if (fCanvas->readPixels(&bitmap, 0, 0)) {
|
|
|
|
SkAutoLockPixels alp(*fBitmap);
|
2013-01-07 18:41:28 +00:00
|
|
|
bitmapCopySubset(bitmap, fBitmap, SkScalarFloorToInt(fRects[i].left()),
|
|
|
|
SkScalarFloorToInt(fRects[i].top()));
|
2012-12-18 22:24:03 +00:00
|
|
|
} else {
|
|
|
|
*fSuccess = false;
|
|
|
|
// If one tile fails to read pixels, do not continue drawing the rest.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 21:28:12 +00:00
|
|
|
}
|
|
|
|
fDone->run();
|
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
void setPathAndSuccess(const SkString* path, bool* success) {
|
|
|
|
fPath = path;
|
|
|
|
fSuccess = success;
|
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
void setBitmap(SkBitmap* bitmap) {
|
|
|
|
fBitmap = bitmap;
|
|
|
|
}
|
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
private:
|
|
|
|
// All pointers unowned.
|
|
|
|
SkPicture* fClone; // Picture to draw from. Each CloneData has a unique one which
|
|
|
|
// is threadsafe.
|
|
|
|
SkCanvas* fCanvas; // Canvas to draw to. Reused for each tile.
|
|
|
|
const SkString* fPath; // If non-null, path to write the result to as a PNG.
|
|
|
|
SkTDArray<SkRect>& fRects; // All tiles of the picture.
|
|
|
|
const int fStart; // Range of tiles drawn by this thread.
|
|
|
|
const int fEnd;
|
|
|
|
bool* fSuccess; // Only meaningful if path is non-null. Shared by all threads,
|
|
|
|
// and only set to false upon failure to write to a PNG.
|
|
|
|
SkRunnable* fDone;
|
2012-12-18 22:24:03 +00:00
|
|
|
SkBitmap* fBitmap;
|
2012-08-31 16:15:22 +00:00
|
|
|
};
|
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
MultiCorePictureRenderer::MultiCorePictureRenderer(int threadCount)
|
|
|
|
: fNumThreads(threadCount)
|
|
|
|
, fThreadPool(threadCount)
|
|
|
|
, fCountdown(threadCount) {
|
|
|
|
// Only need to create fNumThreads - 1 clones, since one thread will use the base
|
|
|
|
// picture.
|
|
|
|
fPictureClones = SkNEW_ARRAY(SkPicture, fNumThreads - 1);
|
|
|
|
fCloneData = SkNEW_ARRAY(CloneData*, fNumThreads);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiCorePictureRenderer::init(SkPicture *pict) {
|
|
|
|
// Set fPicture and the tiles.
|
|
|
|
this->INHERITED::init(pict);
|
|
|
|
for (int i = 0; i < fNumThreads; ++i) {
|
|
|
|
*fCanvasPool.append() = this->setupCanvas(this->getTileWidth(), this->getTileHeight());
|
|
|
|
}
|
|
|
|
// Only need to create fNumThreads - 1 clones, since one thread will use the base picture.
|
|
|
|
fPicture->clone(fPictureClones, fNumThreads - 1);
|
|
|
|
// Populate each thread with the appropriate data.
|
|
|
|
// Group the tiles into nearly equal size chunks, rounding up so we're sure to cover them all.
|
|
|
|
const int chunkSize = (fTileRects.count() + fNumThreads - 1) / fNumThreads;
|
|
|
|
|
|
|
|
for (int i = 0; i < fNumThreads; i++) {
|
|
|
|
SkPicture* pic;
|
|
|
|
if (i == fNumThreads-1) {
|
|
|
|
// The last set will use the original SkPicture.
|
|
|
|
pic = fPicture;
|
|
|
|
} else {
|
|
|
|
pic = &fPictureClones[i];
|
2012-10-03 17:32:33 +00:00
|
|
|
}
|
2012-11-02 21:28:12 +00:00
|
|
|
const int start = i * chunkSize;
|
|
|
|
const int end = SkMin32(start + chunkSize, fTileRects.count());
|
|
|
|
fCloneData[i] = SkNEW_ARGS(CloneData,
|
|
|
|
(pic, fCanvasPool[i], fTileRects, start, end, &fCountdown));
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-31 16:15:22 +00:00
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool MultiCorePictureRenderer::render(const SkString *path, SkBitmap** out) {
|
2012-11-02 21:28:12 +00:00
|
|
|
bool success = true;
|
|
|
|
if (path != NULL) {
|
|
|
|
for (int i = 0; i < fNumThreads-1; i++) {
|
|
|
|
fCloneData[i]->setPathAndSuccess(path, &success);
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
if (NULL != out) {
|
|
|
|
*out = SkNEW(SkBitmap);
|
|
|
|
setup_bitmap(*out, fPicture->width(), fPicture->height());
|
|
|
|
for (int i = 0; i < fNumThreads; i++) {
|
|
|
|
fCloneData[i]->setBitmap(*out);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < fNumThreads; i++) {
|
|
|
|
fCloneData[i]->setBitmap(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
fCountdown.reset(fNumThreads);
|
|
|
|
for (int i = 0; i < fNumThreads; i++) {
|
|
|
|
fThreadPool.add(fCloneData[i]);
|
2012-09-18 14:32:35 +00:00
|
|
|
}
|
2012-11-02 21:28:12 +00:00
|
|
|
fCountdown.wait();
|
2012-09-18 14:32:35 +00:00
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiCorePictureRenderer::end() {
|
|
|
|
for (int i = 0; i < fNumThreads - 1; i++) {
|
|
|
|
SkDELETE(fCloneData[i]);
|
|
|
|
fCloneData[i] = NULL;
|
2012-08-01 17:53:29 +00:00
|
|
|
}
|
2012-11-02 21:28:12 +00:00
|
|
|
|
|
|
|
fCanvasPool.unrefAll();
|
|
|
|
|
|
|
|
this->INHERITED::end();
|
2012-08-01 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 21:28:12 +00:00
|
|
|
MultiCorePictureRenderer::~MultiCorePictureRenderer() {
|
|
|
|
// Each individual CloneData was deleted in end.
|
|
|
|
SkDELETE_ARRAY(fCloneData);
|
|
|
|
SkDELETE_ARRAY(fPictureClones);
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:01:26 +00:00
|
|
|
SkString MultiCorePictureRenderer::getConfigNameInternal() {
|
|
|
|
SkString name = this->INHERITED::getConfigNameInternal();
|
|
|
|
name.appendf("_multi_%i_threads", fNumThreads);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
2012-09-07 15:21:18 +00:00
|
|
|
|
|
|
|
void PlaybackCreationRenderer::setup() {
|
2012-11-02 18:11:49 +00:00
|
|
|
fReplayer.reset(this->createPicture());
|
2012-12-17 19:25:54 +00:00
|
|
|
SkCanvas* recorder = fReplayer->beginRecording(this->getViewWidth(), this->getViewHeight(),
|
2012-11-02 18:11:49 +00:00
|
|
|
this->recordFlags());
|
2012-12-17 19:25:54 +00:00
|
|
|
this->scaleToScaleFactor(recorder);
|
2012-09-07 15:21:18 +00:00
|
|
|
fPicture->draw(recorder);
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool PlaybackCreationRenderer::render(const SkString*, SkBitmap** out) {
|
2012-11-02 18:11:49 +00:00
|
|
|
fReplayer->endRecording();
|
2012-09-20 14:54:21 +00:00
|
|
|
// Since this class does not actually render, return false.
|
|
|
|
return false;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:01:26 +00:00
|
|
|
SkString PlaybackCreationRenderer::getConfigNameInternal() {
|
|
|
|
return SkString("playback_creation");
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:11:49 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SkPicture variants for each BBoxHierarchy type
|
|
|
|
|
|
|
|
class RTreePicture : public SkPicture {
|
|
|
|
public:
|
|
|
|
virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE{
|
|
|
|
static const int kRTreeMinChildren = 6;
|
|
|
|
static const int kRTreeMaxChildren = 11;
|
|
|
|
SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth),
|
|
|
|
SkIntToScalar(fHeight));
|
|
|
|
return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren,
|
|
|
|
aspectRatio);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPicture* PictureRenderer::createPicture() {
|
|
|
|
switch (fBBoxHierarchyType) {
|
|
|
|
case kNone_BBoxHierarchyType:
|
|
|
|
return SkNEW(SkPicture);
|
|
|
|
case kRTree_BBoxHierarchyType:
|
|
|
|
return SkNEW(RTreePicture);
|
2012-11-06 18:58:43 +00:00
|
|
|
case kTileGrid_BBoxHierarchyType:
|
2012-12-13 16:39:53 +00:00
|
|
|
return SkNEW_ARGS(SkTileGridPicture, (fGridWidth, fGridHeight, fPicture->width(),
|
|
|
|
fPicture->height()));
|
2012-11-02 18:11:49 +00:00
|
|
|
}
|
|
|
|
SkASSERT(0); // invalid bbhType
|
|
|
|
return NULL;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
2012-11-02 18:11:49 +00:00
|
|
|
|
2012-11-29 21:00:39 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class GatherRenderer : public PictureRenderer {
|
|
|
|
public:
|
2012-12-18 22:24:03 +00:00
|
|
|
virtual bool render(const SkString* path, SkBitmap** out = NULL)
|
|
|
|
SK_OVERRIDE {
|
2012-11-29 21:00:39 +00:00
|
|
|
SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
|
|
|
|
SkIntToScalar(fPicture->height()));
|
|
|
|
SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds);
|
|
|
|
SkSafeUnref(data);
|
2012-12-11 02:01:20 +00:00
|
|
|
|
2012-11-29 21:00:39 +00:00
|
|
|
return NULL == path; // we don't have anything to write
|
|
|
|
}
|
2012-12-11 02:01:20 +00:00
|
|
|
|
2012-11-29 21:00:39 +00:00
|
|
|
private:
|
|
|
|
virtual SkString getConfigNameInternal() SK_OVERRIDE {
|
|
|
|
return SkString("gather_pixelrefs");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PictureRenderer* CreateGatherPixelRefsRenderer() {
|
|
|
|
return SkNEW(GatherRenderer);
|
|
|
|
}
|
2012-11-30 02:01:24 +00:00
|
|
|
|
2012-12-10 16:05:09 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class PictureCloneRenderer : public PictureRenderer {
|
|
|
|
public:
|
2012-12-18 22:24:03 +00:00
|
|
|
virtual bool render(const SkString* path, SkBitmap** out = NULL)
|
|
|
|
SK_OVERRIDE {
|
2012-12-10 16:05:09 +00:00
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
SkPicture* clone = fPicture->clone();
|
|
|
|
SkSafeUnref(clone);
|
|
|
|
}
|
2012-12-11 02:01:20 +00:00
|
|
|
|
2012-12-10 16:05:09 +00:00
|
|
|
return NULL == path; // we don't have anything to write
|
|
|
|
}
|
2012-12-11 02:01:20 +00:00
|
|
|
|
2012-12-10 16:05:09 +00:00
|
|
|
private:
|
|
|
|
virtual SkString getConfigNameInternal() SK_OVERRIDE {
|
|
|
|
return SkString("picture_clone");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PictureRenderer* CreatePictureCloneRenderer() {
|
|
|
|
return SkNEW(PictureCloneRenderer);
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:11:49 +00:00
|
|
|
} // namespace sk_tools
|