SKPSlide.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include "tools/viewer/SKPSlide.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkStream.h"
  10. #include "src/core/SkOSFile.h"
  11. SKPSlide::SKPSlide(const SkString& name, const SkString& path) : fPath(path) {
  12. fName = name;
  13. }
  14. SKPSlide::~SKPSlide() {}
  15. void SKPSlide::draw(SkCanvas* canvas) {
  16. if (fPic.get()) {
  17. bool isOffset = SkToBool(fCullRect.left() | fCullRect.top());
  18. if (isOffset) {
  19. canvas->save();
  20. canvas->translate(SkIntToScalar(-fCullRect.left()), SkIntToScalar(-fCullRect.top()));
  21. }
  22. canvas->drawPicture(fPic.get());
  23. if (isOffset) {
  24. canvas->restore();
  25. }
  26. }
  27. }
  28. static sk_sp<SkPicture> read_picture(const char path[]) {
  29. std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path);
  30. if (!stream) {
  31. SkDebugf("Could not read %s.\n", path);
  32. return nullptr;
  33. }
  34. auto pic = SkPicture::MakeFromStream(stream.get());
  35. if (!pic) {
  36. SkDebugf("Could not read %s as an SkPicture.\n", path);
  37. }
  38. return pic;
  39. }
  40. void SKPSlide::load(SkScalar, SkScalar) {
  41. fPic = read_picture(fPath.c_str());
  42. fCullRect = fPic->cullRect().roundOut();
  43. }
  44. void SKPSlide::unload() {
  45. fPic.reset(nullptr);
  46. }