2012-06-04 12:05:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkDumpCanvas.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "Sk64.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
2012-12-10 14:12:55 +00:00
|
|
|
#include "SkOSFile.h"
|
2012-06-04 12:05:43 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkXMLParser.h"
|
|
|
|
|
|
|
|
class PictFileView : public SampleView {
|
|
|
|
SkString fFilename;
|
|
|
|
SkPicture* fPicture;
|
2012-12-10 14:12:55 +00:00
|
|
|
SkPicture* fBBoxPicture;
|
|
|
|
bool fUseBBox;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-12-10 14:12:55 +00:00
|
|
|
static SkPicture* LoadPicture(const char path[], bool useBBox) {
|
2012-08-03 13:39:57 +00:00
|
|
|
SkPicture* pic = NULL;
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
if (SkImageDecoder::DecodeFile(path, &bm)) {
|
|
|
|
bm.setImmutable();
|
|
|
|
pic = SkNEW(SkPicture);
|
|
|
|
SkCanvas* can = pic->beginRecording(bm.width(), bm.height());
|
|
|
|
can->drawBitmap(bm, 0, 0, NULL);
|
|
|
|
pic->endRecording();
|
|
|
|
} else {
|
|
|
|
SkFILEStream stream(path);
|
|
|
|
if (stream.isValid()) {
|
2012-11-09 02:01:24 +00:00
|
|
|
pic = SkNEW_ARGS(SkPicture,
|
2012-11-08 13:46:32 +00:00
|
|
|
(&stream, NULL, &SkImageDecoder::DecodeStream));
|
2012-08-03 13:39:57 +00:00
|
|
|
}
|
2012-08-31 17:14:46 +00:00
|
|
|
|
2012-08-31 13:32:47 +00:00
|
|
|
if (false) { // re-record
|
|
|
|
SkPicture p2;
|
|
|
|
pic->draw(p2.beginRecording(pic->width(), pic->height()));
|
|
|
|
p2.endRecording();
|
2012-08-31 17:14:46 +00:00
|
|
|
|
2012-08-31 13:32:47 +00:00
|
|
|
SkString path2(path);
|
|
|
|
path2.append(".new.skp");
|
|
|
|
SkFILEWStream writer(path2.c_str());
|
|
|
|
p2.serialize(&writer);
|
|
|
|
}
|
2012-08-03 13:39:57 +00:00
|
|
|
}
|
2012-12-10 14:12:55 +00:00
|
|
|
|
|
|
|
if (useBBox) {
|
|
|
|
SkPicture* bboxPicture = SkNEW(SkPicture);
|
|
|
|
pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
|
|
|
|
SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
|
|
|
|
bboxPicture->endRecording();
|
|
|
|
SkDELETE(pic);
|
|
|
|
return bboxPicture;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return pic;
|
|
|
|
}
|
2012-08-03 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 12:05:43 +00:00
|
|
|
public:
|
|
|
|
PictFileView(const char name[] = NULL) : fFilename(name) {
|
|
|
|
fPicture = NULL;
|
2012-12-10 14:12:55 +00:00
|
|
|
fBBoxPicture = NULL;
|
|
|
|
fUseBBox = false;
|
2012-06-04 12:05:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-06-04 12:05:43 +00:00
|
|
|
virtual ~PictFileView() {
|
|
|
|
SkSafeUnref(fPicture);
|
2012-12-10 14:12:55 +00:00
|
|
|
SkSafeUnref(fBBoxPicture);
|
2012-06-04 12:05:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-06-04 12:05:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SkString name("P:");
|
2012-12-10 14:12:55 +00:00
|
|
|
const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
|
|
|
|
name.append(basename ? basename+1: fFilename.c_str());
|
|
|
|
if (fUseBBox) {
|
|
|
|
name.append(" <bbox>");
|
|
|
|
}
|
2012-06-04 12:05:43 +00:00
|
|
|
SampleCode::TitleR(evt, name.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-12-10 14:12:55 +00:00
|
|
|
virtual bool onEvent(const SkEvent& evt) {
|
|
|
|
if (evt.isType("PictFileView::toggleBBox")) {
|
|
|
|
fUseBBox = !fUseBBox;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onEvent(evt);
|
|
|
|
}
|
|
|
|
|
2012-06-04 12:05:43 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2012-12-10 14:12:55 +00:00
|
|
|
SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture;
|
|
|
|
|
|
|
|
if (!*picture) {
|
|
|
|
*picture = LoadPicture(fFilename.c_str(), fUseBBox);
|
2012-08-03 13:39:57 +00:00
|
|
|
}
|
2012-12-10 14:12:55 +00:00
|
|
|
if (*picture) {
|
|
|
|
canvas->drawPicture(**picture);
|
2012-06-04 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-03 13:39:57 +00:00
|
|
|
private:
|
2012-06-04 12:05:43 +00:00
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
SampleView* CreateSamplePictFileView(const char filename[]);
|
|
|
|
SampleView* CreateSamplePictFileView(const char filename[]) {
|
|
|
|
return new PictFileView(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static SkView* MyFactory() { return new PictFileView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
#endif
|
|
|
|
|