1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/viewer/SKPSlide.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPicture.h"
12 #include "include/core/SkStream.h"
13 #include "include/core/SkString.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkTo.h"
16
17 #include <utility>
18
SKPSlide(const SkString & name,const SkString & path)19 SKPSlide::SKPSlide(const SkString& name, const SkString& path)
20 : SKPSlide(name, SkStream::MakeFromFile(path.c_str())) {
21 }
22
SKPSlide(const SkString & name,std::unique_ptr<SkStream> stream)23 SKPSlide::SKPSlide(const SkString& name, std::unique_ptr<SkStream> stream)
24 : fStream(std::move(stream)) {
25 fName = name;
26 }
27
~SKPSlide()28 SKPSlide::~SKPSlide() {}
29
draw(SkCanvas * canvas)30 void SKPSlide::draw(SkCanvas* canvas) {
31 if (fPic) {
32 bool isOffset = SkToBool(fCullRect.left() | fCullRect.top());
33 if (isOffset) {
34 canvas->save();
35 canvas->translate(SkIntToScalar(-fCullRect.left()), SkIntToScalar(-fCullRect.top()));
36 }
37
38 canvas->drawPicture(fPic.get());
39
40 if (isOffset) {
41 canvas->restore();
42 }
43 }
44 }
45
load(SkScalar,SkScalar)46 void SKPSlide::load(SkScalar, SkScalar) {
47 if (!fStream) {
48 SkDebugf("No skp stream for slide %s.\n", fName.c_str());
49 return;
50 }
51 fStream->rewind();
52 fPic = SkPicture::MakeFromStream(fStream.get());
53 if (!fPic) {
54 SkDebugf("Could not parse SkPicture from skp stream for slide %s.\n", fName.c_str());
55 return;
56 }
57 fCullRect = fPic->cullRect().roundOut();
58 }
59
unload()60 void SKPSlide::unload() {
61 fPic.reset(nullptr);
62 }
63