2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
2009-12-04 21:32:27 +00:00
|
|
|
#include "SkDumpCanvas.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "Sk64.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#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"
|
|
|
|
|
2010-04-22 16:07:49 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-11-10 15:54:55 +00:00
|
|
|
#include "SkImageRef_GlobalPool.h"
|
|
|
|
|
|
|
|
static SkBitmap load_bitmap() {
|
|
|
|
SkBitmap bm;
|
2013-09-25 21:34:24 +00:00
|
|
|
SkStreamAsset* stream = SkStream::NewFromFile("/skimages/sesame_street_ensemble-hp.jpg");
|
2013-05-29 19:22:20 +00:00
|
|
|
if (stream) {
|
|
|
|
SkAutoUnref aur(stream);
|
|
|
|
|
|
|
|
if (SkImageDecoder::DecodeStream(stream, &bm, SkBitmap::kNo_Config,
|
|
|
|
SkImageDecoder::kDecodeBounds_Mode)) {
|
|
|
|
SkPixelRef* pr = new SkImageRef_GlobalPool(stream, bm.config(), 1);
|
|
|
|
bm.setPixelRef(pr)->unref();
|
|
|
|
}
|
2009-11-10 15:54:55 +00:00
|
|
|
}
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static void drawCircle(SkCanvas* canvas, int r, SkColor color) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(color);
|
|
|
|
|
|
|
|
canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r),
|
|
|
|
paint);
|
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
class PictureView : public SampleView {
|
2009-11-10 15:54:55 +00:00
|
|
|
SkBitmap fBitmap;
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-04-16 15:50:18 +00:00
|
|
|
PictureView() {
|
2009-11-10 15:54:55 +00:00
|
|
|
SkImageRef_GlobalPool::SetRAMBudget(16 * 1024);
|
|
|
|
|
|
|
|
fBitmap = load_bitmap();
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPicture = new SkPicture;
|
|
|
|
SkCanvas* canvas = fPicture->beginRecording(100, 100);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-11-10 15:54:55 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, NULL);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
drawCircle(canvas, 50, SK_ColorBLACK);
|
|
|
|
fSubPicture = new SkPicture;
|
|
|
|
canvas->drawPicture(*fSubPicture);
|
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
|
|
|
canvas->drawPicture(*fSubPicture);
|
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
|
|
|
canvas->drawPicture(*fSubPicture);
|
|
|
|
canvas->translate(SkIntToScalar(-50), 0);
|
|
|
|
canvas->drawPicture(*fSubPicture);
|
|
|
|
// fPicture now has (4) references to us. We can release ours, and just
|
|
|
|
// unref fPicture in our destructor, and it will in turn take care of
|
|
|
|
// the other references to fSubPicture
|
|
|
|
fSubPicture->unref();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual ~PictureView() {
|
|
|
|
fPicture->unref();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Picture");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawSomething(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
2009-11-10 15:54:55 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2012-04-16 15:50:18 +00:00
|
|
|
canvas->scale(SkFloatToScalar(0.5f), SkFloatToScalar(0.5f));
|
2009-11-10 15:54:55 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, NULL);
|
|
|
|
canvas->restore();
|
|
|
|
|
2009-12-04 21:32:27 +00:00
|
|
|
const char beforeStr[] = "before circle";
|
|
|
|
const char afterStr[] = "after circle";
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
2009-12-04 21:32:27 +00:00
|
|
|
canvas->drawData(beforeStr, sizeof(beforeStr));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
|
|
|
|
SkIntToScalar(40), paint);
|
2009-12-04 21:32:27 +00:00
|
|
|
canvas->drawData(afterStr, sizeof(afterStr));
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setTextSize(SkIntToScalar(40));
|
|
|
|
canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
|
|
|
|
paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
drawSomething(canvas);
|
2009-05-22 14:39:03 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPicture* pict = new SkPicture;
|
|
|
|
SkAutoUnref aur(pict);
|
|
|
|
|
|
|
|
drawSomething(pict->beginRecording(100, 100));
|
|
|
|
pict->endRecording();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
|
|
|
|
canvas->scale(-SK_Scalar1, -SK_Scalar1);
|
|
|
|
canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
|
|
|
|
canvas->drawPicture(*pict);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
|
|
|
|
canvas->scale(SK_Scalar1, -SK_Scalar1);
|
|
|
|
canvas->translate(0, -SkIntToScalar(50));
|
|
|
|
canvas->drawPicture(*pict);
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
canvas->scale(-SK_Scalar1, SK_Scalar1);
|
|
|
|
canvas->translate(-SkIntToScalar(100), 0);
|
|
|
|
canvas->drawPicture(*pict);
|
|
|
|
canvas->restore();
|
2009-12-04 21:32:27 +00:00
|
|
|
|
2013-01-15 20:17:47 +00:00
|
|
|
#ifdef SK_DEVELOPER
|
2009-12-04 21:32:27 +00:00
|
|
|
if (false) {
|
|
|
|
SkDebugfDumper dumper;
|
|
|
|
SkDumpCanvas dumpCanvas(&dumper);
|
|
|
|
dumpCanvas.drawPicture(*pict);
|
|
|
|
}
|
2013-01-15 20:17:47 +00:00
|
|
|
#endif
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// test that we can re-record a subpicture, and see the results
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand(SampleCode::GetAnimTime());
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
|
|
|
|
drawCircle(fSubPicture->beginRecording(50, 50), 25,
|
2009-11-23 21:07:51 +00:00
|
|
|
rand.nextU() | 0xFF000000);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPicture(*fPicture);
|
|
|
|
delayInval(500);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
#define INVAL_ALL_TYPE "inval-all"
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void delayInval(SkMSec delay) {
|
2011-08-04 13:50:17 +00:00
|
|
|
(new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual bool onEvent(const SkEvent& evt) {
|
|
|
|
if (evt.isType(INVAL_ALL_TYPE)) {
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onEvent(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPicture* fPicture;
|
|
|
|
SkPicture* fSubPicture;
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new PictureView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|