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