skia2/samplecode/SamplePicture.cpp

202 lines
5.7 KiB
C++
Raw Normal View History

Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
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.
*/
#include "gm.h"
#include "Resources.h"
#include "SampleCode.h"
#include "SkCanvas.h"
#include "SkColorFilter.h"
#include "SkColorPriv.h"
#include "SkData.h"
#include "SkDecodingImageGenerator.h"
#include "SkDumpCanvas.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkOSFile.h"
#include "SkPath.h"
#include "SkPicture.h"
#include "SkPictureRecorder.h"
#include "SkRandom.h"
#include "SkRegion.h"
#include "SkShader.h"
#include "SkStream.h"
#include "SkTime.h"
#include "SkTypeface.h"
#include "SkUtils.h"
#include "SkView.h"
#include "SkXMLParser.h"
#include "SkXfermode.h"
///////////////////////////////////////////////////////////////////////////////
static SkBitmap load_bitmap() {
SkBitmap bm;
SkString pngFilename = GetResourcePath("mandrill_512.png");
SkAutoDataUnref data(SkData::NewFromFileName(pngFilename.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 13:15:13 +00:00
SkInstallDiscardablePixelRef(SkDecodingImageGenerator::Create(
data, SkDecodingImageGenerator::Options()), &bm);
}
return bm;
}
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);
}
class PictureView : public SampleView {
SkBitmap fBitmap;
public:
PictureView() {
fBitmap = load_bitmap();
SkPictureRecorder recorder;
recorder.beginRecording(100, 100, NULL, 0);
fSubPicture = recorder.endRecording();
SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
SkPaint paint;
paint.setAntiAlias(true);
canvas->drawBitmap(fBitmap, 0, 0, NULL);
drawCircle(canvas, 50, SK_ColorBLACK);
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 = recorder.endRecording();
// fPicture now has (4) references to fSubPicture. We can release our ref,
// and just unref fPicture in our destructor, and it will in turn take care of
// the other references to fSubPicture
fSubPicture->unref();
}
virtual ~PictureView() {
fPicture->unref();
}
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;
canvas->save();
canvas->scale(0.5f, 0.5f);
canvas->drawBitmap(fBitmap, 0, 0, NULL);
canvas->restore();
const char beforeStr[] = "before circle";
const char afterStr[] = "after circle";
paint.setAntiAlias(true);
paint.setColor(SK_ColorRED);
canvas->drawData(beforeStr, sizeof(beforeStr));
canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
SkIntToScalar(40), paint);
canvas->drawData(afterStr, sizeof(afterStr));
paint.setColor(SK_ColorBLACK);
paint.setTextSize(SkIntToScalar(40));
canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
paint);
}
virtual void onDrawContent(SkCanvas* canvas) {
this->drawSomething(canvas);
SkPictureRecorder recorder;
this->drawSomething(recorder.beginRecording(100, 100, NULL, 0));
SkAutoTUnref<SkPicture> pict(recorder.endRecording());
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();
canvas->save();
canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
canvas->scale(-SK_Scalar1, SK_Scalar1);
canvas->translate(-SkIntToScalar(100), 0);
canvas->drawPicture(pict);
canvas->restore();
#ifdef SK_DEVELOPER
if (false) {
SkDebugfDumper dumper;
SkDumpCanvas dumpCanvas(&dumper);
dumpCanvas.drawPicture(pict);
}
#endif
// This used to re-record the sub-picture and redraw the parent
// A capability that is now forbidden!
SkRandom rand(SampleCode::GetAnimTime());
canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
canvas->drawPicture(fPicture);
delayInval(500);
}
private:
#define INVAL_ALL_TYPE "inval-all"
void delayInval(SkMSec delay) {
(new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
}
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;
typedef SampleView INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new PictureView; }
static SkViewRegister reg(MyFactory);