2012-07-26 17:27:57 +00:00
|
|
|
#include "PictureRenderer.h"
|
|
|
|
#include "SamplePipeControllers.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkGPipe.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkTDArray.h"
|
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "picture_utils.h"
|
|
|
|
|
2012-08-20 15:03:29 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#endif
|
|
|
|
|
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() {
|
|
|
|
fCanvas->flush();
|
|
|
|
|
|
|
|
if (this->isUsingGpuDevice()) {
|
|
|
|
SkGLContext* glContext = fGrContextFactory.getGLContext(
|
|
|
|
GrContextFactory::kNative_GLContextType);
|
|
|
|
SK_GL(*glContext, Finish());
|
|
|
|
fGrContext->freeGpuResources();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void PipePictureRenderer::render() {
|
|
|
|
SkASSERT(fCanvas.get() != NULL);
|
|
|
|
SkASSERT(fPicture != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
if (NULL == fCanvas.get() || NULL == fPicture) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-07 17:11:33 +00:00
|
|
|
void SimplePictureRenderer::render() {
|
|
|
|
SkASSERT(fCanvas.get() != NULL);
|
|
|
|
SkASSERT(fPicture != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
if (NULL == fCanvas.get() || NULL == fPicture) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fCanvas->drawPicture(*fPicture);
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TiledPictureRenderer::TiledPictureRenderer()
|
|
|
|
: fTileWidth(kDefaultTileWidth)
|
|
|
|
, fTileHeight(kDefaultTileHeight) {}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::init(SkPicture* pict) {
|
|
|
|
SkASSERT(pict != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
SkASSERT(0 == fTiles.count());
|
|
|
|
if (NULL == pict || fTiles.count() != 0) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->INHERITED::init(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-07 17:11:33 +00:00
|
|
|
this->setupTiles();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::render() {
|
|
|
|
SkASSERT(fCanvas.get() != NULL);
|
|
|
|
SkASSERT(fPicture != NULL);
|
2012-08-20 15:03:44 +00:00
|
|
|
if (NULL == fCanvas.get() || NULL == fPicture) {
|
2012-08-07 17:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->drawTiles();
|
|
|
|
this->copyTilesToCanvas();
|
|
|
|
}
|
2012-07-26 17:27:57 +00:00
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::end() {
|
|
|
|
this->deleteTiles();
|
|
|
|
this->INHERITED::end();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TiledPictureRenderer::~TiledPictureRenderer() {
|
2012-08-07 17:11:33 +00:00
|
|
|
this->deleteTiles();
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::clipTile(const TileInfo& tile) {
|
|
|
|
SkRect clip = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
|
|
|
|
SkIntToScalar(fPicture->height()));
|
2012-07-26 17:27:57 +00:00
|
|
|
tile.fCanvas->clipRect(clip);
|
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::addTile(int tile_x_start, int tile_y_start) {
|
2012-07-26 17:27:57 +00:00
|
|
|
TileInfo* tile = fTiles.push();
|
|
|
|
|
2012-08-20 15:03:57 +00:00
|
|
|
tile->fCanvas = this->setupCanvas(fTileWidth, fTileHeight);
|
2012-07-26 17:27:57 +00:00
|
|
|
tile->fCanvas->translate(SkIntToScalar(-tile_x_start), SkIntToScalar(-tile_y_start));
|
2012-08-07 17:11:33 +00:00
|
|
|
this->clipTile(*tile);
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::setupTiles() {
|
|
|
|
for (int tile_y_start = 0; tile_y_start < fPicture->height();
|
2012-07-26 17:27:57 +00:00
|
|
|
tile_y_start += fTileHeight) {
|
2012-08-07 17:11:33 +00:00
|
|
|
for (int tile_x_start = 0; tile_x_start < fPicture->width();
|
2012-07-26 17:27:57 +00:00
|
|
|
tile_x_start += fTileWidth) {
|
2012-08-07 17:11:33 +00:00
|
|
|
this->addTile(tile_x_start, tile_y_start);
|
2012-07-26 17:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TiledPictureRenderer::deleteTiles() {
|
|
|
|
for (int i = 0; i < fTiles.count(); ++i) {
|
|
|
|
SkDELETE(fTiles[i].fCanvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
fTiles.reset();
|
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::drawTiles() {
|
2012-08-01 17:53:29 +00:00
|
|
|
for (int i = 0; i < fTiles.count(); ++i) {
|
2012-08-07 17:11:33 +00:00
|
|
|
fTiles[i].fCanvas->drawPicture(*(fPicture));
|
2012-08-01 17:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 15:04:00 +00:00
|
|
|
void TiledPictureRenderer::resetState() {
|
|
|
|
for (int i = 0; i < fTiles.count(); ++i) {
|
|
|
|
fTiles[i].fCanvas->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->INHERITED::resetState();
|
|
|
|
}
|
|
|
|
|
2012-08-07 17:11:33 +00:00
|
|
|
void TiledPictureRenderer::copyTilesToCanvas() {
|
2012-07-26 17:27:57 +00:00
|
|
|
int tile_index = 0;
|
2012-08-07 17:11:33 +00:00
|
|
|
for (int tile_y_start = 0; tile_y_start < fPicture->height();
|
2012-07-26 17:27:57 +00:00
|
|
|
tile_y_start += fTileHeight) {
|
2012-08-07 17:11:33 +00:00
|
|
|
for (int tile_x_start = 0; tile_x_start < fPicture->width();
|
2012-07-26 17:27:57 +00:00
|
|
|
tile_x_start += fTileWidth) {
|
|
|
|
SkASSERT(tile_index < fTiles.count());
|
|
|
|
SkBitmap source = fTiles[tile_index].fCanvas->getDevice()->accessBitmap(false);
|
2012-08-07 17:11:33 +00:00
|
|
|
fCanvas->drawBitmap(source,
|
|
|
|
SkIntToScalar(tile_x_start),
|
|
|
|
SkIntToScalar(tile_y_start));
|
2012-07-26 17:27:57 +00:00
|
|
|
++tile_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|