jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 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 "SKPSlide.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 11 | #include "SkCommonFlags.h" |
| 12 | #include "SkOSFile.h" |
| 13 | #include "SkStream.h" |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 14 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 15 | SKPSlide::SKPSlide(const SkString& name, const SkString& path) : fPath(path) { |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 16 | fName = name; |
| 17 | } |
| 18 | |
| 19 | SKPSlide::~SKPSlide() {} |
| 20 | |
| 21 | void SKPSlide::draw(SkCanvas* canvas) { |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 22 | if (fPic.get()) { |
| 23 | bool isOffset = SkToBool(fCullRect.left() | fCullRect.top()); |
| 24 | if (isOffset) { |
| 25 | canvas->save(); |
| 26 | canvas->translate(SkIntToScalar(-fCullRect.left()), SkIntToScalar(-fCullRect.top())); |
| 27 | } |
| 28 | |
| 29 | canvas->drawPicture(fPic.get()); |
| 30 | |
| 31 | if (isOffset) { |
| 32 | canvas->restore(); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static sk_sp<SkPicture> read_picture(const char path[]) { |
| 38 | SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path)); |
| 39 | if (stream.get() == nullptr) { |
| 40 | SkDebugf("Could not read %s.\n", path); |
| 41 | return nullptr; |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 42 | } |
| 43 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 44 | auto pic = SkPicture::MakeFromStream(stream.get()); |
| 45 | if (!pic) { |
| 46 | SkDebugf("Could not read %s as an SkPicture.\n", path); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 47 | } |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 48 | return pic; |
| 49 | } |
| 50 | |
| 51 | void SKPSlide::load() { |
| 52 | fPic = read_picture(fPath.c_str()); |
| 53 | fCullRect = fPic->cullRect().roundOut(); |
| 54 | } |
| 55 | |
| 56 | void SKPSlide::unload() { |
| 57 | fPic.reset(nullptr); |
jvanverth | 2bb3b6d | 2016-04-08 07:24:09 -0700 | [diff] [blame] | 58 | } |