skia2/samplecode/SampleAnimator.cpp

174 lines
4.4 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 "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkAnimator.h"
#include "SkStream.h"
#include "SkDOM.h"
#include <memory>
///////////////////////////////////////////////////////////////////////////////
class SkAnimatorView : public SkView {
public:
SkAnimatorView();
virtual ~SkAnimatorView();
void setURIBase(const char dir[]);
SkAnimator* getAnimator() const { return fAnimator; }
bool decodeFile(const char path[]);
bool decodeMemory(const void* buffer, size_t size);
bool decodeStream(SkStream* stream);
protected:
// overrides
virtual void onDraw(SkCanvas*);
virtual bool onQuery(SkEvent* evt);
private:
SkString fBaseURI;
SkAnimator* fAnimator;
typedef SkView INHERITED;
};
SkAnimatorView::SkAnimatorView() : fAnimator(nullptr) {}
SkAnimatorView::~SkAnimatorView() {
delete fAnimator;
}
void SkAnimatorView::setURIBase(const char dir[]) {
fBaseURI.set(dir);
}
bool SkAnimatorView::decodeFile(const char path[]) {
std::unique_ptr<SkStream> is = SkStream::MakeFromFile(path);
return is && this->decodeStream(is.get());
}
bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) {
Make SkStream *not* ref counted. SkStream is a stateful object, so it does not make sense for it to have multiple owners. Make SkStream inherit directly from SkNoncopyable. Update methods which previously called SkStream::ref() (e.g. SkImageDecoder::buildTileIndex() and SkFrontBufferedStream::Create(), which required the existing owners to call SkStream::unref()) to take ownership of their SkStream parameters and delete when done (including on failure). Switch all SkAutoTUnref<SkStream>s to SkAutoTDelete<SkStream>s. In some cases this means heap allocating streams that were previously stack allocated. Respect ownership rules of SkTypeface::CreateFromStream() and SkImageDecoder::buildTileIndex(). Update the comments for exceptional methods which do not affect the ownership of their SkStream parameters (e.g. SkPicture::CreateFromStream() and SkTypeface::Deserialize()) to be explicit about ownership. Remove test_stream_life, which tested that buildTileIndex() behaved correctly when SkStream was a ref counted object. The test does not make sense now that it is not. In SkPDFStream, remove the SkMemoryStream member. Instead of using it, create a new SkMemoryStream to pass to fDataStream (which is now an SkAutoTDelete). Make other pdf rasterizers behave like SkPDFDocumentToBitmap. SkPDFDocumentToBitmap delete the SkStream, so do the same in the following pdf rasterizers: SkPopplerRasterizePDF SkNativeRasterizePDF SkNoRasterizePDF Requires a change to Android, which currently treats SkStreams as ref counted objects. Review URL: https://codereview.chromium.org/849103004
2015-01-21 20:09:53 +00:00
SkMemoryStream is(buffer, size);
return this->decodeStream(&is);
}
static const SkDOMNode* find_nodeID(const SkDOM& dom,
const SkDOMNode* node, const char name[]) {
if (nullptr == node) {
node = dom.getRootNode();
}
do {
const char* idval = dom.findAttr(node, "id");
if (idval && !strcmp(idval, name)) {
return node;
}
const SkDOMNode* child = dom.getFirstChild(node);
if (child) {
const SkDOMNode* found = find_nodeID(dom, child, name);
if (found) {
return found;
}
}
} while ((node = dom.getNextSibling(node)) != nullptr);
return nullptr;
}
bool SkAnimatorView::decodeStream(SkStream* stream) {
delete fAnimator;
fAnimator = new SkAnimator;
fAnimator->setURIBase(fBaseURI.c_str());
#if 0
if (!fAnimator->decodeStream(stream)) {
delete fAnimator;
fAnimator = nullptr;
return false;
}
#else
size_t len = stream->getLength();
char* text = (char*)sk_malloc_throw(len);
stream->read(text, len);
SkDOM dom;
const SkDOM::Node* root = dom.build(text, len);
if (nullptr == root) {
return false;
}
if (!fAnimator->decodeDOM(dom, root)) {
delete fAnimator;
fAnimator = nullptr;
return false;
}
for (int i = 0; i <= 10; i++) {
SkString name("glyph");
name.appendS32(i);
const SkDOM::Node* node = find_nodeID(dom, nullptr, name.c_str());
SkASSERT(node);
SkRect r;
dom.findScalar(node, "left", &r.fLeft);
dom.findScalar(node, "top", &r.fTop);
dom.findScalar(node, "width", &r.fRight); r.fRight += r.fLeft;
dom.findScalar(node, "height", &r.fBottom); r.fBottom += r.fTop;
SkDebugf("--- %s [%g %g %g %g]\n", name.c_str(),
r.fLeft, r.fTop, r.fRight, r.fBottom);
}
#endif
return true;
}
#include "SkTime.h"
void SkAnimatorView::onDraw(SkCanvas* canvas) {
canvas->drawColor(SK_ColorWHITE);
if (fAnimator) {
fAnimator->draw(canvas, 0);
#if 0
canvas->save();
canvas->translate(120, 30);
canvas->scale(0.5, 0.5);
fAnimator->draw(canvas, 0);
canvas->restore();
canvas->save();
canvas->translate(190, 40);
canvas->scale(0.25, 0.25);
fAnimator->draw(canvas, 0);
canvas->restore();
this->inval(nullptr);
#endif
}
}
bool SkAnimatorView::onQuery(SkEvent* evt) {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "Animator");
return true;
}
return this->INHERITED::onQuery(evt);
}
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() {
SkAnimatorView* av = new SkAnimatorView;
// av->decodeFile("/skimages/test.xml");
#if 0
av->setURIBase("/skia/trunk/animations/");
av->decodeFile("/skia/trunk/animations/checkbox.xml");
#else
av->setURIBase("/");
av->decodeFile("/testanim.txt");
#endif
return av;
}
static SkViewRegister reg(MyFactory);