Add SkTileGridPicture SampleApp playback support.

SampleApp already supports switching to a bbox hierarchy playback mode
('b' shortcut). This CL adds an SkTileGridPicture bbox mode.

R=reed@google.com, robertphillips@google.com, fmalita@google.com

Author: fmalita@chromium.org

Review URL: https://codereview.chromium.org/108513006

git-svn-id: http://skia.googlecode.com/svn/trunk@12613 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2013-12-10 21:51:06 +00:00
parent edd370f949
commit bbe43a9ce0
4 changed files with 132 additions and 69 deletions

View File

@ -671,7 +671,7 @@ struct TilingInfo {
SkScalar w, h;
};
static struct TilingInfo gTilingInfo[] = {
static const struct TilingInfo gTilingInfo[] = {
{ "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
{ "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
{ "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
@ -682,6 +682,12 @@ static struct TilingInfo gTilingInfo[] = {
SK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
Incomplete_tiling_labels);
SkSize SampleWindow::tileSize() const {
SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
const struct TilingInfo* info = gTilingInfo + fTilingMode;
return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
info->h > SK_Scalar1 ? info->h : this->height() * info->h);
}
//////////////////////////////////////////////////////////////////////////////
static SkView* curr_view(SkWindow* wind) {
@ -1222,11 +1228,7 @@ void SampleWindow::draw(SkCanvas* canvas) {
if (bitmap_diff(canvas, orig, &diff)) {
}
} else {
SkSize tile;
SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
struct TilingInfo* info = gTilingInfo + fTilingMode;
tile.set(info->w > SK_Scalar1 ? info->w : width() * info->w,
info->h > SK_Scalar1 ? info->h : height() * info->h);
SkSize tile = this->tileSize();
for (SkScalar y = 0; y < height(); y += tile.height()) {
for (SkScalar x = 0; x < width(); x += tile.width()) {
@ -1760,8 +1762,15 @@ bool SampleWindow::onEvent(const SkEvent& evt) {
SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev) ||
SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
this->inval(NULL);
this->updateTitle();
return true;
}
if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
if (SampleView::IsSampleView(curr_view(this))) {
((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
}
this->inval(NULL);
this->updateTitle();
return true;
@ -2121,8 +2130,11 @@ void SampleWindow::loadView(SkView* view) {
fSlideMenu->reset();
(void)SampleView::SetUsePipe(view, fPipeState);
if (SampleView::IsSampleView(view))
((SampleView*)view)->requestMenu(fSlideMenu);
if (SampleView::IsSampleView(view)) {
SampleView* sampleView = (SampleView*)view;
sampleView->requestMenu(fSlideMenu);
sampleView->onTileSizeChanged(this->tileSize());
}
this->onUpdateMenu(fSlideMenu);
this->updateTitle();
}
@ -2275,6 +2287,10 @@ void SampleWindow::onSizeChange() {
#endif
this->updateTitle(); // to refresh our config
fDevManager->windowSizeChanged(this);
if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
((SampleView*)view)->onTileSizeChanged(this->tileSize());
}
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -243,6 +243,7 @@ private:
void installDrawFilter(SkCanvas*);
int findByTitle(const char*);
void listTitles();
SkSize tileSize() const;
typedef SkOSWindow INHERITED;
};

View File

@ -129,6 +129,8 @@ public:
*/
virtual void requestMenu(SkOSMenu* menu) {}
virtual void onTileSizeChanged(const SkSize& tileSize) {}
protected:
virtual void onDrawBackground(SkCanvas*);
virtual void onDrawContent(SkCanvas*) = 0;

View File

@ -19,6 +19,7 @@
#include "SkRandom.h"
#include "SkRegion.h"
#include "SkShader.h"
#include "SkTileGridPicture.h"
#include "SkUtils.h"
#include "SkColorPriv.h"
#include "SkColorFilter.h"
@ -31,12 +32,81 @@
#include "SkXMLParser.h"
class PictFileView : public SampleView {
SkString fFilename;
SkPicture* fPicture;
SkPicture* fBBoxPicture;
bool fUseBBox;
public:
PictFileView(const char name[] = NULL)
: fFilename(name)
, fBBox(kNo_BBoxType)
, fTileSize(SkSize::Make(0, 0)) {
for (unsigned i = 0; i < kBBoxTypeCount; ++i) {
fPictures[i] = NULL;
}
}
static SkPicture* LoadPicture(const char path[], bool useBBox) {
virtual ~PictFileView() {
for (unsigned i = 0; i < kBBoxTypeCount; ++i) {
SkSafeUnref(fPictures[i]);
}
}
virtual void onTileSizeChanged(const SkSize &tileSize) SK_OVERRIDE {
if (tileSize != fTileSize) {
fTileSize = tileSize;
SkSafeSetNull(fPictures[kTileGrid_BBoxType]);
}
}
protected:
// overrides from SkEventSink
virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
if (SampleCode::TitleQ(*evt)) {
SkString name("P:");
const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
name.append(basename ? basename+1: fFilename.c_str());
if (fBBox != kNo_BBoxType) {
name.append(fBBox == kRTree_BBoxType ? " <bbox: R>" : " <bbox: T>");
}
SampleCode::TitleR(evt, name.c_str());
return true;
}
return this->INHERITED::onQuery(evt);
}
virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE {
if (evt.isType("PictFileView::toggleBBox")) {
fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
return true;
}
return this->INHERITED::onEvent(evt);
}
virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
SkASSERT(fBBox < kBBoxTypeCount);
SkPicture** picture = fPictures + fBBox;
if (!*picture) {
*picture = LoadPicture(fFilename.c_str(), fBBox);
}
if (*picture) {
canvas->drawPicture(**picture);
}
}
private:
enum BBoxType {
kNo_BBoxType,
kRTree_BBoxType,
kTileGrid_BBoxType,
kLast_BBoxType = kTileGrid_BBoxType
};
static const unsigned kBBoxTypeCount = kLast_BBoxType + 1;
SkString fFilename;
SkPicture* fPictures[kBBoxTypeCount];
BBoxType fBBox;
SkSize fTileSize;
SkPicture* LoadPicture(const char path[], BBoxType bbox) {
SkPicture* pic = NULL;
SkBitmap bm;
@ -71,67 +141,41 @@ class PictFileView : public SampleView {
}
}
if (useBBox) {
SkPicture* bboxPicture = SkNEW(SkPicture);
if (!pic) {
return NULL;
}
SkPicture* bboxPicture = NULL;
switch (bbox) {
case kNo_BBoxType:
// no bbox playback necessary
break;
case kRTree_BBoxType:
bboxPicture = SkNEW(SkPicture);
break;
case kTileGrid_BBoxType: {
SkASSERT(!fTileSize.isEmpty());
SkTileGridPicture::TileGridInfo gridInfo;
gridInfo.fMargin = SkISize::Make(0, 0);
gridInfo.fOffset = SkIPoint::Make(0, 0);
gridInfo.fTileInterval = fTileSize.toRound();
bboxPicture = SkNEW_ARGS(SkTileGridPicture, (pic->width(), pic->height(), gridInfo));
} break;
default:
SkASSERT(false);
}
if (bboxPicture) {
pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
bboxPicture->endRecording();
SkDELETE(pic);
return bboxPicture;
} else {
return pic;
}
return pic;
}
public:
PictFileView(const char name[] = NULL) : fFilename(name) {
fPicture = NULL;
fBBoxPicture = NULL;
fUseBBox = false;
}
virtual ~PictFileView() {
SkSafeUnref(fPicture);
SkSafeUnref(fBBoxPicture);
}
protected:
// overrides from SkEventSink
virtual bool onQuery(SkEvent* evt) {
if (SampleCode::TitleQ(*evt)) {
SkString name("P:");
const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
name.append(basename ? basename+1: fFilename.c_str());
if (fUseBBox) {
name.append(" <bbox>");
}
SampleCode::TitleR(evt, name.c_str());
return true;
}
return this->INHERITED::onQuery(evt);
}
virtual bool onEvent(const SkEvent& evt) {
if (evt.isType("PictFileView::toggleBBox")) {
fUseBBox = !fUseBBox;
return true;
}
return this->INHERITED::onEvent(evt);
}
virtual void onDrawContent(SkCanvas* canvas) {
SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture;
if (!*picture) {
*picture = LoadPicture(fFilename.c_str(), fUseBBox);
}
if (*picture) {
canvas->drawPicture(**picture);
}
}
private:
typedef SampleView INHERITED;
};