2011-07-28 18:26:00 +04: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.
|
|
|
|
*/
|
2014-01-13 18:53:55 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
#include "SampleCode.h"
|
2013-12-18 19:08:35 +04:00
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkDecodingImageGenerator.h"
|
2009-12-05 00:32:27 +03:00
|
|
|
#include "SkDumpCanvas.h"
|
2008-12-17 18:59:43 +03:00
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
2013-12-18 19:08:35 +04:00
|
|
|
#include "SkOSFile.h"
|
2008-12-17 18:59:43 +03:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPicture.h"
|
2014-04-18 22:04:41 +04:00
|
|
|
#include "SkPictureRecorder.h"
|
2008-12-17 18:59:43 +03:00
|
|
|
#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"
|
|
|
|
|
2013-12-18 19:08:35 +04:00
|
|
|
#include "gm.h"
|
2010-04-22 20:07:49 +04:00
|
|
|
|
2013-12-18 19:08:35 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2009-11-10 18:54:55 +03:00
|
|
|
|
|
|
|
static SkBitmap load_bitmap() {
|
|
|
|
SkBitmap bm;
|
2013-12-18 19:08:35 +04:00
|
|
|
SkString directory = skiagm::GM::GetResourcePath();
|
|
|
|
SkString path = SkOSPath::SkPathJoin(directory.c_str(), "mandrill_512.png");
|
|
|
|
SkAutoDataUnref data(SkData::NewFromFileName(path.c_str()));
|
|
|
|
if (data.get() != NULL) {
|
Add Options to SkDecodingImageGenerator, simplify API.
Motivation: We want to remove redundant classes from Skia. To
that end we want to remove SkImageRef and its subclasses and
replace their uses with SkDiscardablePixelRef +
SkDecodingImageGenerator. Since Android uses SkImageRef, we need
to make sure that SkDecodingImageGenerator allows all of the
settings that Android exposes in BitmapFactory.Options.
To that end, we have created an Options struct for the
SkDecodingImageGenerator which lets the client of the generator set
sample size, dithering, and bitmap config.
We have made the SkDecodingImageGenerator constructor private
and replaced the SkDecodingImageGenerator::Install functions
with a SkDecodingImageGenerator::Create functions (one for
SkData and one for SkStream) which now take a
SkDecodingImageGenerator::Options struct.
Also added a ImageDecoderOptions test which loops through a list
of sets of options and tries them on a set of 5 small encoded
images.
Also updated several users of SkDecodingImageGenerator::Install to
follow new call signature - gm/factory.cpp, LazyDecodeBitmap.cpp,
and PictureTest.cpp, CachedDecodingPixelRefTest.cpp.
We also added a new ImprovedBitmapFactory Test which simulates the
exact function that Android will need to modify to use this,
installPixelRef() in BitmapFactory.
R=reed@google.com, scroggo@google.com
Committed: https://code.google.com/p/skia/source/detail?r=12744
Review URL: https://codereview.chromium.org/93703004
git-svn-id: http://skia.googlecode.com/svn/trunk@12855 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-02 17:15:13 +04:00
|
|
|
SkInstallDiscardablePixelRef(SkDecodingImageGenerator::Create(
|
2014-05-27 18:14:22 +04:00
|
|
|
data, SkDecodingImageGenerator::Options()), &bm);
|
2009-11-10 18:54:55 +03:00
|
|
|
}
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2008-12-17 18:59:43 +03: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 05:59:48 +04:00
|
|
|
class PictureView : public SampleView {
|
2009-11-10 18:54:55 +03:00
|
|
|
SkBitmap fBitmap;
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2013-12-18 19:08:35 +04:00
|
|
|
PictureView() {
|
2009-11-10 18:54:55 +03:00
|
|
|
|
|
|
|
fBitmap = load_bitmap();
|
|
|
|
|
2014-04-13 23:09:42 +04:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
|
2014-04-18 03:35:06 +04:00
|
|
|
recorder.beginRecording(100, 100, NULL, 0);
|
2014-04-13 23:09:42 +04:00
|
|
|
fSubPicture = recorder.endRecording();
|
|
|
|
|
2014-04-18 03:35:06 +04:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
|
2008-12-17 18:59:43 +03:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2009-11-10 18:54:55 +03:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, NULL);
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
drawCircle(canvas, 50, SK_ColorBLACK);
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(fSubPicture);
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(fSubPicture);
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->translate(0, SkIntToScalar(50));
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(fSubPicture);
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->translate(SkIntToScalar(-50), 0);
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(fSubPicture);
|
2014-04-13 23:09:42 +04:00
|
|
|
|
|
|
|
fPicture = recorder.endRecording();
|
|
|
|
|
2014-04-14 07:04:57 +04:00
|
|
|
// fPicture now has (4) references to fSubPicture. We can release our ref,
|
2014-04-13 23:09:42 +04:00
|
|
|
// and just unref fPicture in our destructor, and it will in turn take care of
|
2008-12-17 18:59:43 +03:00
|
|
|
// the other references to fSubPicture
|
|
|
|
fSubPicture->unref();
|
|
|
|
}
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
virtual ~PictureView() {
|
|
|
|
fPicture->unref();
|
|
|
|
}
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03: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 18:54:55 +03:00
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 23:44:07 +04:00
|
|
|
canvas->scale(0.5f, 0.5f);
|
2009-11-10 18:54:55 +03:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, NULL);
|
|
|
|
canvas->restore();
|
|
|
|
|
2009-12-05 00:32:27 +03:00
|
|
|
const char beforeStr[] = "before circle";
|
|
|
|
const char afterStr[] = "after circle";
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
paint.setColor(SK_ColorRED);
|
2009-12-05 00:32:27 +03:00
|
|
|
canvas->drawData(beforeStr, sizeof(beforeStr));
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
|
|
|
|
SkIntToScalar(40), paint);
|
2009-12-05 00:32:27 +03:00
|
|
|
canvas->drawData(afterStr, sizeof(afterStr));
|
2008-12-17 18:59:43 +03:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setTextSize(SkIntToScalar(40));
|
|
|
|
canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
|
|
|
|
paint);
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
2011-05-05 05:59:48 +04:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2014-04-13 23:09:42 +04:00
|
|
|
this->drawSomething(canvas);
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2014-04-13 23:09:42 +04:00
|
|
|
SkPictureRecorder recorder;
|
2014-04-18 03:35:06 +04:00
|
|
|
this->drawSomething(recorder.beginRecording(100, 100, NULL, 0));
|
2014-04-13 23:09:42 +04:00
|
|
|
SkAutoTUnref<SkPicture> pict(recorder.endRecording());
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
|
|
|
|
canvas->scale(-SK_Scalar1, -SK_Scalar1);
|
|
|
|
canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(pict);
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
|
|
|
|
canvas->scale(SK_Scalar1, -SK_Scalar1);
|
|
|
|
canvas->translate(0, -SkIntToScalar(50));
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(pict);
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->restore();
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
canvas->scale(-SK_Scalar1, SK_Scalar1);
|
|
|
|
canvas->translate(-SkIntToScalar(100), 0);
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(pict);
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->restore();
|
2009-12-05 00:32:27 +03:00
|
|
|
|
2013-01-16 00:17:47 +04:00
|
|
|
#ifdef SK_DEVELOPER
|
2009-12-05 00:32:27 +03:00
|
|
|
if (false) {
|
|
|
|
SkDebugfDumper dumper;
|
|
|
|
SkDumpCanvas dumpCanvas(&dumper);
|
2014-06-04 16:40:44 +04:00
|
|
|
dumpCanvas.drawPicture(pict);
|
2009-12-05 00:32:27 +03:00
|
|
|
}
|
2013-01-16 00:17:47 +04:00
|
|
|
#endif
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2014-04-13 23:09:42 +04:00
|
|
|
// This used to re-record the sub-picture and redraw the parent
|
|
|
|
// A capability that is now forbidden!
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2013-09-10 00:09:12 +04:00
|
|
|
SkRandom rand(SampleCode::GetAnimTime());
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
|
2014-06-04 16:40:44 +04:00
|
|
|
canvas->drawPicture(fPicture);
|
2008-12-17 18:59:43 +03:00
|
|
|
delayInval(500);
|
|
|
|
}
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
private:
|
|
|
|
#define INVAL_ALL_TYPE "inval-all"
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
void delayInval(SkMSec delay) {
|
2011-08-04 17:50:17 +04:00
|
|
|
(new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03: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 05:59:48 +04:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 18:59:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new PictureView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|