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/SkStream.h"
12 #include "src/core/SkOSFile.h"
13
SKPSlide(const SkString & name,const SkString & path)14 SKPSlide::SKPSlide(const SkString& name, const SkString& path)
15 : SKPSlide(name, SkStream::MakeFromFile(path.c_str())) {
16 }
17
SKPSlide(const SkString & name,std::unique_ptr<SkStream> stream)18 SKPSlide::SKPSlide(const SkString& name, std::unique_ptr<SkStream> stream)
19 : fStream(std::move(stream)) {
20 fName = name;
21 }
22
~SKPSlide()23 SKPSlide::~SKPSlide() {}
24
draw(SkCanvas * canvas)25 void SKPSlide::draw(SkCanvas* canvas) {
26 if (fPic) {
27 bool isOffset = SkToBool(fCullRect.left() | fCullRect.top());
28 if (isOffset) {
29 canvas->save();
30 canvas->translate(SkIntToScalar(-fCullRect.left()), SkIntToScalar(-fCullRect.top()));
31 }
32
33 canvas->drawPicture(fPic.get());
34
35 if (isOffset) {
36 canvas->restore();
37 }
38 }
39 }
40
load(SkScalar,SkScalar)41 void SKPSlide::load(SkScalar, SkScalar) {
42 if (!fStream) {
43 SkDebugf("No skp stream for slide %s.\n", fName.c_str());
44 return;
45 }
46 fStream->rewind();
47 fPic = SkPicture::MakeFromStream(fStream.get());
48 if (!fPic) {
49 SkDebugf("Could parse SkPicture from skp stream for slide %s.\n", fName.c_str());
50 return;
51 }
52 fCullRect = fPic->cullRect().roundOut();
53 }
54
unload()55 void SKPSlide::unload() {
56 fPic.reset(nullptr);
57 }
58