mtklein | 60c0475 | 2015-07-08 10:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkData.h" |
| 9 | #include "SkForceLinking.h" |
| 10 | #include "SkImageGenerator.h" |
| 11 | #include "SkPicture.h" |
| 12 | #include "SkPictureRecorder.h" |
| 13 | #include "SkStream.h" |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 17 | |
| 18 | #define ASSERTF(cond, fmt, ...) if (!(cond)) { fprintf(stderr, fmt"\n", __VA_ARGS__); exit(1); } |
| 19 | |
| 20 | static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { |
| 21 | SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); |
| 22 | return encoded && SkInstallDiscardablePixelRef(encoded, dst); |
| 23 | } |
| 24 | |
| 25 | int main(int argc, char** argv) { |
| 26 | ASSERTF(argc == 3, "usage: %s nested.skp flat.skp", argv[0]); |
| 27 | const char *nestedPath = argv[1], |
| 28 | *flatPath = argv[2]; |
| 29 | |
| 30 | // Read nested.skp. |
| 31 | SkFILEStream stream(nestedPath); |
| 32 | ASSERTF(stream.isValid(), "Couldn't read %s.", nestedPath); |
| 33 | SkAutoTUnref<const SkPicture> nested(SkPicture::CreateFromStream(&stream, &lazy_decode_bitmap)); |
| 34 | ASSERTF(nested, "Couldn't parse %s as a picture.", nestedPath); |
| 35 | |
| 36 | // Play it back into a new picture using kPlaybackDrawPicture_RecordFlag. |
| 37 | SkPictureRecorder recorder; |
| 38 | uint32_t flags = SkPictureRecorder::kPlaybackDrawPicture_RecordFlag; |
| 39 | nested->playback(recorder.beginRecording(nested->cullRect(), nullptr, flags)); |
| 40 | SkAutoTUnref<const SkPicture> flat(recorder.endRecordingAsPicture()); |
| 41 | |
| 42 | // Write out that flat.skp |
| 43 | SkFILEWStream wstream(flatPath); |
| 44 | ASSERTF(wstream.isValid(), "Could not open %s.", flatPath); |
| 45 | flat->serialize(&wstream); |
| 46 | wstream.flush(); |
| 47 | |
| 48 | return 0; |
| 49 | } |