| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright 2017 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 "skia/ext/SkFutureDrawable.h" |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "third_party/skia/include/core/SkCanvas.h" |
| 12 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 13 #include "third_party/skia/include/core/SkScalar.h" |
| 14 #include "third_party/skia/include/core/SkWriteBuffer.h" |
| 15 #include "third_party/skia/src/core/SkReadBuffer.h" |
| 16 |
| 17 const char SkFutureDrawable::kTypeName[] = "SkFutureDrawable"; |
| 18 |
| 19 std::unordered_map<int, std::unique_ptr<SkDrawable>> |
| 20 SkFutureDrawable::fDrawableRefMap = {}; |
| 21 |
| 22 void SkFutureDrawable::flatten(SkWriteBuffer& buffer) const { |
| 23 // Write the bounds. |
| 24 buffer.writeRect(fBounds); |
| 25 // Write the unique id. |
| 26 buffer.writeInt(id); |
| 27 } |
| 28 |
| 29 // static |
| 30 sk_sp<SkFlattenable> SkFutureDrawable::CreateProc(SkReadBuffer& buffer) { |
| 31 // Read the bounds. |
| 32 SkRect bounds; |
| 33 buffer.readRect(&bounds); |
| 34 |
| 35 // Read the unique id. |
| 36 int id = buffer.readInt(); |
| 37 sk_sp<SkFutureDrawable> drawable = sk_make_sp<SkFutureDrawable>(bounds, id); |
| 38 |
| 39 // Check whether the drawable reference is there |
| 40 const auto ptr = fDrawableRefMap.find(id); |
| 41 if (ptr != fDrawableRefMap.end()) |
| 42 drawable->setDrawableRef(ptr->second.get()); |
| 43 |
| 44 return drawable; |
| 45 } |
| 46 |
| 47 void SkFutureDrawable::onDraw(SkCanvas* canvas) { |
| 48 if (!fDrawableRef) |
| 49 return; |
| 50 fDrawableRef->draw(canvas); |
| 51 } |
| 52 |
| 53 SkPicture* SkFutureDrawable::onNewPictureSnapshot() { |
| 54 SkPictureRecorder recorder; |
| 55 |
| 56 SkCanvas* canvas = recorder.beginRecording(fBounds, nullptr, 0); |
| 57 canvas->translate(fBounds.x(), fBounds.y()); |
| 58 draw(canvas); |
| 59 return recorder.finishRecordingAsPicture().release(); |
| 60 } |
| OLD | NEW |