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-08-23 15:24:13 +00:00
|
|
|
#include "SkMatrix.h"
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "SkPicture.h"
|
2012-08-23 15:24:13 +00:00
|
|
|
#include "SkScalar.h"
|
2012-08-21 19:05:08 +00:00
|
|
|
#include "SkString.h"
|
2012-08-31 16:15:22 +00:00
|
|
|
#include "SkTemplates.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-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-08-20 15:03:57 +00:00
|
|
|
fCanvas.reset(this->setupCanvas());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* PictureRenderer::setupCanvas() {
|
|
|
|
return this->setupCanvas(fPicture->width(), fPicture->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* PictureRenderer::setupCanvas(int width, int height) {
|
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);
|
|
|
|
return SkNEW_ARGS(SkCanvas, (bitmap));
|
2012-08-20 15:03:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#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)));
|
|
|
|
return SkNEW_ARGS(SkCanvas, (device.get()));
|
2012-08-20 15:03:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
2012-08-20 15:03:57 +00:00
|
|
|
|
|
|
|
return NULL;
|
2012-08-07 17:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PictureRenderer::end() {
|
2012-08-20 15:03:47 +00:00
|
|
|
this->resetState();
|
2012-08-07 17:11:33 +00:00
|
|
|
fPicture = NULL;
|
|
|
|
fCanvas.reset(NULL);
|
|
|
|
}
|
|
|
|
|
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-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-09-20 14:54:21 +00:00
|
|
|
bool RecordPictureRenderer::render(const SkString*) {
|
2012-09-07 15:21:18 +00:00
|
|
|
SkPicture replayer;
|
|
|
|
SkCanvas* recorder = replayer.beginRecording(fPicture->width(), fPicture->height());
|
|
|
|
fPicture->draw(recorder);
|
|
|
|
replayer.endRecording();
|
2012-09-20 14:54:21 +00:00
|
|
|
// Since this class does not actually render, return false.
|
|
|
|
return false;
|
2012-09-07 15:21:18 +00:00
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-09-20 14:54:21 +00:00
|
|
|
bool PipePictureRenderer::render(const SkString* path) {
|
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);
|
|
|
|
}
|
|
|
|
return true;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-09-20 14:54:21 +00:00
|
|
|
bool SimplePictureRenderer::render(const SkString* path) {
|
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);
|
|
|
|
}
|
|
|
|
return true;
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-26 17:27:57 +00:00
|
|
|
TiledPictureRenderer::TiledPictureRenderer()
|
2012-09-20 14:42:33 +00:00
|
|
|
: fUsePipe(false)
|
2012-08-31 16:15:22 +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)
|
2012-09-20 14:42:33 +00:00
|
|
|
, fTileMinPowerOf2Width(0)
|
|
|
|
, fTileCounter(0)
|
|
|
|
, fNumThreads(1)
|
|
|
|
, fPictureClones(NULL)
|
|
|
|
, fPipeController(NULL) { }
|
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-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();
|
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
|
|
|
|
if (this->multiThreaded()) {
|
|
|
|
for (int i = 0; i < fNumThreads; ++i) {
|
|
|
|
*fCanvasPool.append() = this->setupCanvas(fTileWidth, fTileHeight);
|
|
|
|
}
|
|
|
|
if (!fUsePipe) {
|
|
|
|
SkASSERT(NULL == fPictureClones);
|
|
|
|
// Only need to create fNumThreads - 1 clones, since one thread will use the base
|
|
|
|
// picture.
|
|
|
|
int numberOfClones = fNumThreads - 1;
|
|
|
|
// This will be deleted in end().
|
|
|
|
fPictureClones = SkNEW_ARRAY(SkPicture, numberOfClones);
|
2012-10-01 20:06:09 +00:00
|
|
|
fPicture->clone(fPictureClones, numberOfClones);
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
SkDELETE_ARRAY(fPictureClones);
|
|
|
|
fPictureClones = NULL;
|
|
|
|
fCanvasPool.unrefAll();
|
|
|
|
if (fPipeController != NULL) {
|
|
|
|
SkASSERT(fUsePipe);
|
|
|
|
SkDELETE(fPipeController);
|
|
|
|
fPipeController = NULL;
|
|
|
|
}
|
2012-08-07 17:11:33 +00:00
|
|
|
this->INHERITED::end();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TiledPictureRenderer::~TiledPictureRenderer() {
|
2012-09-20 14:42:33 +00:00
|
|
|
// end() must be called to delete fPictureClones and fPipeController
|
|
|
|
SkASSERT(NULL == fPictureClones);
|
|
|
|
SkASSERT(NULL == fPipeController);
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::setupTiles() {
|
2012-09-18 14:32:35 +00:00
|
|
|
for (int tile_y_start = 0; tile_y_start < fPicture->height(); tile_y_start += fTileHeight) {
|
|
|
|
for (int tile_x_start = 0; tile_x_start < fPicture->width(); tile_x_start += fTileWidth) {
|
|
|
|
*fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
|
|
|
|
SkIntToScalar(tile_y_start),
|
|
|
|
SkIntToScalar(fTileWidth),
|
|
|
|
SkIntToScalar(fTileHeight));
|
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() {
|
|
|
|
int rounded_value = fPicture->width();
|
|
|
|
if (fPicture->width() % fTileMinPowerOf2Width != 0) {
|
|
|
|
rounded_value = fPicture->width() - (fPicture->width() % fTileMinPowerOf2Width)
|
|
|
|
+ fTileMinPowerOf2Width;
|
|
|
|
}
|
|
|
|
|
2012-09-06 18:43:21 +00:00
|
|
|
int num_bits = SkScalarCeilToInt(SkScalarLog2(SkIntToScalar(fPicture->width())));
|
2012-08-23 20:53:25 +00:00
|
|
|
int largest_possible_tile_size = 1 << num_bits;
|
|
|
|
|
|
|
|
// The tile height is constant for a particular picture.
|
|
|
|
for (int tile_y_start = 0; tile_y_start < fPicture->height(); tile_y_start += fTileHeight) {
|
|
|
|
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) {
|
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();
|
|
|
|
// Translate so that we draw the correct portion of the picture
|
|
|
|
canvas->translate(-tileRect.fLeft, -tileRect.fTop);
|
|
|
|
playback->draw(canvas);
|
|
|
|
canvas->restoreToCount(saveCount);
|
|
|
|
canvas->flush();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Base class for data used both by pipe and clone picture multi threaded drawing.
|
|
|
|
|
|
|
|
struct ThreadData {
|
2012-10-03 17:32:33 +00:00
|
|
|
ThreadData(SkCanvas* target, int* tileCounter, SkTDArray<SkRect>* tileRects,
|
|
|
|
const SkString* path, bool* success)
|
2012-09-20 14:42:33 +00:00
|
|
|
: fCanvas(target)
|
2012-10-03 17:32:33 +00:00
|
|
|
, fPath(path)
|
2012-10-11 19:10:33 +00:00
|
|
|
, fSuccess(success)
|
|
|
|
, fTileCounter(tileCounter)
|
|
|
|
, fTileRects(tileRects) {
|
2012-09-20 14:42:33 +00:00
|
|
|
SkASSERT(target != NULL && tileCounter != NULL && tileRects != NULL);
|
2012-10-03 17:32:33 +00:00
|
|
|
// Success must start off true, and it will be set to false upon failure.
|
|
|
|
SkASSERT(success != NULL && *success);
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 17:32:33 +00:00
|
|
|
int32_t nextTile(SkRect* rect) {
|
2012-10-01 20:06:09 +00:00
|
|
|
int32_t i = sk_atomic_inc(fTileCounter);
|
|
|
|
if (i < fTileRects->count()) {
|
2012-10-03 17:32:33 +00:00
|
|
|
SkASSERT(rect != NULL);
|
|
|
|
*rect = fTileRects->operator[](i);
|
|
|
|
return i;
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
2012-10-03 17:32:33 +00:00
|
|
|
return -1;
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All of these are pointers to objects owned elsewhere
|
|
|
|
SkCanvas* fCanvas;
|
2012-10-03 17:32:33 +00:00
|
|
|
const SkString* fPath;
|
|
|
|
bool* fSuccess;
|
2012-09-20 14:42:33 +00:00
|
|
|
private:
|
|
|
|
// Shared by all threads, this states which is the next tile to be drawn.
|
|
|
|
int32_t* fTileCounter;
|
|
|
|
// Points to the array of rectangles. The array is already created before any threads are
|
|
|
|
// started and then it is unmodified, so there is no danger of race conditions.
|
|
|
|
const SkTDArray<SkRect>* fTileRects;
|
|
|
|
};
|
|
|
|
|
2012-08-31 16:15:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Draw using Pipe
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
struct TileData : public ThreadData {
|
|
|
|
TileData(ThreadSafePipeController* controller, SkCanvas* canvas, int* tileCounter,
|
2012-10-03 17:32:33 +00:00
|
|
|
SkTDArray<SkRect>* tileRects, const SkString* path, bool* success)
|
|
|
|
: INHERITED(canvas, tileCounter, tileRects, path, success)
|
2012-09-20 14:42:33 +00:00
|
|
|
, fController(controller) {}
|
|
|
|
|
2012-08-31 16:25:46 +00:00
|
|
|
ThreadSafePipeController* fController;
|
2012-09-20 14:42:33 +00:00
|
|
|
|
|
|
|
typedef ThreadData INHERITED;
|
2012-08-31 16:15:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void DrawTile(void* data) {
|
|
|
|
SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024);
|
|
|
|
TileData* tileData = static_cast<TileData*>(data);
|
|
|
|
|
2012-10-03 17:32:33 +00:00
|
|
|
SkRect tileRect;
|
|
|
|
int32_t i;
|
|
|
|
while ((i = tileData->nextTile(&tileRect)) != -1) {
|
|
|
|
DrawTileToCanvas(tileData->fCanvas, tileRect, tileData->fController);
|
2012-10-26 13:26:55 +00:00
|
|
|
if (NULL != tileData->fPath &&
|
|
|
|
!writeAppendNumber(tileData->fCanvas, tileData->fPath, i)) {
|
2012-10-03 17:32:33 +00:00
|
|
|
*tileData->fSuccess = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
SkDELETE(tileData);
|
|
|
|
}
|
2012-08-31 16:15:22 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Draw using Picture
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
struct CloneData : public ThreadData {
|
2012-10-03 17:32:33 +00:00
|
|
|
CloneData(SkPicture* clone, SkCanvas* target, int* tileCounter, SkTDArray<SkRect>* tileRects,
|
|
|
|
const SkString* path, bool* success)
|
|
|
|
: INHERITED(target, tileCounter, tileRects, path, success)
|
2012-09-20 14:42:33 +00:00
|
|
|
, fClone(clone) {}
|
|
|
|
|
2012-08-31 16:15:22 +00:00
|
|
|
SkPicture* fClone;
|
2012-09-20 14:42:33 +00:00
|
|
|
|
|
|
|
typedef ThreadData INHERITED;
|
2012-08-31 16:15:22 +00:00
|
|
|
};
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
static void DrawClonedTiles(void* data) {
|
2012-08-31 16:15:22 +00:00
|
|
|
SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024);
|
|
|
|
CloneData* cloneData = static_cast<CloneData*>(data);
|
|
|
|
|
2012-10-03 17:32:33 +00:00
|
|
|
SkRect tileRect;
|
|
|
|
int32_t i;
|
|
|
|
while ((i = cloneData->nextTile(&tileRect)) != -1) {
|
|
|
|
DrawTileToCanvas(cloneData->fCanvas, tileRect, cloneData->fClone);
|
2012-10-26 13:26:55 +00:00
|
|
|
if (NULL != cloneData->fPath &&
|
|
|
|
!writeAppendNumber(cloneData->fCanvas, cloneData->fPath, i)) {
|
2012-10-03 17:32:33 +00:00
|
|
|
*cloneData->fSuccess = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
}
|
|
|
|
SkDELETE(cloneData);
|
|
|
|
}
|
2012-08-31 16:15:22 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
void TiledPictureRenderer::setup() {
|
|
|
|
if (this->multiThreaded()) {
|
|
|
|
// Reset to zero so we start with the first tile.
|
|
|
|
fTileCounter = 0;
|
|
|
|
if (fUsePipe) {
|
|
|
|
// Record the picture into the pipe controller. It is done here because unlike
|
|
|
|
// SkPicture, the pipe is modified (bitmaps can be removed) by drawing.
|
|
|
|
// fPipeController is deleted here after each call to render() except the last one and
|
|
|
|
// in end() for the last one.
|
|
|
|
if (fPipeController != NULL) {
|
|
|
|
SkDELETE(fPipeController);
|
|
|
|
}
|
|
|
|
fPipeController = SkNEW_ARGS(ThreadSafePipeController, (fTileRects.count()));
|
|
|
|
SkGPipeWriter writer;
|
|
|
|
SkCanvas* pipeCanvas = writer.startRecording(fPipeController,
|
|
|
|
SkGPipeWriter::kSimultaneousReaders_Flag);
|
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
fPicture->draw(pipeCanvas);
|
|
|
|
writer.endRecording();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:54:21 +00:00
|
|
|
bool TiledPictureRenderer::render(const SkString* path) {
|
2012-09-18 14:32:35 +00:00
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
if (NULL == fPicture) {
|
2012-09-20 14:54:21 +00:00
|
|
|
return false;
|
2012-09-18 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
if (this->multiThreaded()) {
|
|
|
|
SkASSERT(fCanvasPool.count() == fNumThreads);
|
|
|
|
SkTDArray<SkThread*> threads;
|
|
|
|
SkThread::entryPointProc proc = fUsePipe ? DrawTile : DrawClonedTiles;
|
2012-10-03 17:32:33 +00:00
|
|
|
bool success = true;
|
2012-09-20 14:42:33 +00:00
|
|
|
for (int i = 0; i < fNumThreads; ++i) {
|
|
|
|
// data will be deleted by the entryPointProc.
|
|
|
|
ThreadData* data;
|
|
|
|
if (fUsePipe) {
|
2012-10-03 17:32:33 +00:00
|
|
|
data = SkNEW_ARGS(TileData, (fPipeController, fCanvasPool[i], &fTileCounter,
|
|
|
|
&fTileRects, path, &success));
|
2012-09-20 14:42:33 +00:00
|
|
|
} else {
|
|
|
|
SkPicture* pic = (0 == i) ? fPicture : &fPictureClones[i-1];
|
2012-10-03 17:32:33 +00:00
|
|
|
data = SkNEW_ARGS(CloneData, (pic, fCanvasPool[i], &fTileCounter, &fTileRects, path,
|
|
|
|
&success));
|
2012-08-31 16:15:22 +00:00
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
SkThread* thread = SkNEW_ARGS(SkThread, (proc, data));
|
|
|
|
if (!thread->start()) {
|
|
|
|
SkDebugf("Could not start %s thread %i.\n", (fUsePipe ? "pipe" : "picture"), i);
|
2012-08-31 16:15:22 +00:00
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
*threads.append() = thread;
|
2012-08-31 16:15:22 +00:00
|
|
|
}
|
2012-09-20 14:42:33 +00:00
|
|
|
SkASSERT(threads.count() == fNumThreads);
|
|
|
|
for (int i = 0; i < fNumThreads; ++i) {
|
|
|
|
SkThread* thread = threads[i];
|
|
|
|
thread->join();
|
|
|
|
SkDELETE(thread);
|
|
|
|
}
|
|
|
|
threads.reset();
|
2012-10-03 17:32:33 +00:00
|
|
|
return success;
|
2012-08-31 16:15:22 +00:00
|
|
|
} else {
|
2012-09-20 14:42:33 +00:00
|
|
|
// For single thread, we really only need one canvas total.
|
2012-09-18 14:32:35 +00:00
|
|
|
SkCanvas* canvas = this->setupCanvas(fTileWidth, fTileHeight);
|
|
|
|
SkAutoUnref aur(canvas);
|
|
|
|
|
2012-10-26 13:26:55 +00:00
|
|
|
bool success = true;
|
2012-09-18 14:32:35 +00:00
|
|
|
for (int i = 0; i < fTileRects.count(); ++i) {
|
2012-09-20 14:42:33 +00:00
|
|
|
DrawTileToCanvas(canvas, fTileRects[i], fPicture);
|
2012-10-26 13:26:55 +00:00
|
|
|
if (NULL != path) {
|
|
|
|
success &= writeAppendNumber(canvas, path, i);
|
2012-09-18 14:32:35 +00:00
|
|
|
}
|
2012-08-31 16:15:22 +00:00
|
|
|
}
|
2012-10-26 13:26:55 +00:00
|
|
|
return success;
|
2012-08-01 17:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:42:33 +00:00
|
|
|
SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
|
|
|
|
SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
|
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
// Clip the tile to an area that is completely in what the SkPicture says is the
|
|
|
|
// drawn-to area. 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.
|
|
|
|
SkRect clip = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
|
|
|
|
SkIntToScalar(fPicture->height()));
|
|
|
|
canvas->clipRect(clip);
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:32:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
2012-09-07 15:21:18 +00:00
|
|
|
|
|
|
|
void PlaybackCreationRenderer::setup() {
|
|
|
|
SkCanvas* recorder = fReplayer.beginRecording(fPicture->width(), fPicture->height());
|
|
|
|
fPicture->draw(recorder);
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:54:21 +00:00
|
|
|
bool PlaybackCreationRenderer::render(const SkString*) {
|
2012-09-07 15:21:18 +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
|
|
|
}
|
|
|
|
|
|
|
|
}
|