SkRecordedDrawable.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "include/core/SkMatrix.h"
  8. #include "include/core/SkPictureRecorder.h"
  9. #include "src/core/SkPictureData.h"
  10. #include "src/core/SkPicturePlayback.h"
  11. #include "src/core/SkPictureRecord.h"
  12. #include "src/core/SkRecordDraw.h"
  13. #include "src/core/SkRecordedDrawable.h"
  14. void SkRecordedDrawable::onDraw(SkCanvas* canvas) {
  15. SkDrawable* const* drawables = nullptr;
  16. int drawableCount = 0;
  17. if (fDrawableList) {
  18. drawables = fDrawableList->begin();
  19. drawableCount = fDrawableList->count();
  20. }
  21. SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, fBBH.get(), nullptr);
  22. }
  23. SkPicture* SkRecordedDrawable::onNewPictureSnapshot() {
  24. SkBigPicture::SnapshotArray* pictList = nullptr;
  25. if (fDrawableList) {
  26. // TODO: should we plumb-down the BBHFactory and recordFlags from our host
  27. // PictureRecorder?
  28. pictList = fDrawableList->newDrawableSnapshot();
  29. }
  30. size_t subPictureBytes = 0;
  31. for (int i = 0; pictList && i < pictList->count(); i++) {
  32. subPictureBytes += pictList->begin()[i]->approximateBytesUsed();
  33. }
  34. // SkBigPicture will take ownership of a ref on both fRecord and fBBH.
  35. // We're not willing to give up our ownership, so we must ref them for SkPicture.
  36. return new SkBigPicture(fBounds, SkRef(fRecord.get()), pictList, SkSafeRef(fBBH.get()),
  37. subPictureBytes);
  38. }
  39. void SkRecordedDrawable::flatten(SkWriteBuffer& buffer) const {
  40. // Write the bounds.
  41. buffer.writeRect(fBounds);
  42. // Create an SkPictureRecord to record the draw commands.
  43. SkPictInfo info;
  44. SkPictureRecord pictureRecord(SkISize::Make(fBounds.width(), fBounds.height()), 0);
  45. // If the query contains the whole picture, don't bother with the bounding box hierarchy.
  46. SkBBoxHierarchy* bbh;
  47. if (pictureRecord.getLocalClipBounds().contains(fBounds)) {
  48. bbh = nullptr;
  49. } else {
  50. bbh = fBBH.get();
  51. }
  52. // Record the draw commands.
  53. pictureRecord.beginRecording();
  54. SkRecordDraw(*fRecord, &pictureRecord, nullptr, fDrawableList->begin(), fDrawableList->count(),
  55. bbh, nullptr);
  56. pictureRecord.endRecording();
  57. // Flatten the recorded commands and drawables.
  58. SkPictureData pictureData(pictureRecord, info);
  59. pictureData.flatten(buffer);
  60. }
  61. sk_sp<SkFlattenable> SkRecordedDrawable::CreateProc(SkReadBuffer& buffer) {
  62. // Read the bounds.
  63. SkRect bounds;
  64. buffer.readRect(&bounds);
  65. // Unflatten into a SkPictureData.
  66. SkPictInfo info;
  67. info.setVersion(buffer.getVersion());
  68. info.fCullRect = bounds;
  69. std::unique_ptr<SkPictureData> pictureData(SkPictureData::CreateFromBuffer(buffer, info));
  70. if (!pictureData) {
  71. return nullptr;
  72. }
  73. // Create a drawable.
  74. SkPicturePlayback playback(pictureData.get());
  75. SkPictureRecorder recorder;
  76. playback.draw(recorder.beginRecording(bounds), nullptr, &buffer);
  77. return recorder.finishRecordingAsDrawable();
  78. }