Flutter Engine
The Flutter Engine
Functions
MultiPictureDocumentTest.cpp File Reference
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkDocument.h"
#include "include/core/SkFont.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPicture.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkRRect.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkSerialProcs.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTextBlob.h"
#include "include/core/SkTypeface.h"
#include "include/docs/SkMultiPictureDocument.h"
#include "tests/Test.h"
#include "tools/SkSharingProc.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include <memory>
#include <vector>

Go to the source code of this file.

Functions

static void draw_basic (SkCanvas *canvas, int seed, sk_sp< SkImage > image)
 
static void draw_advanced (SkCanvas *canvas, int seed, sk_sp< SkImage > image, sk_sp< SkPicture > sub)
 
 DEF_TEST (SkMultiPictureDocument_Serialize_and_deserialize, reporter)
 

Function Documentation

◆ DEF_TEST()

DEF_TEST ( SkMultiPictureDocument_Serialize_and_deserialize  ,
reporter   
)

Definition at line 97 of file MultiPictureDocumentTest.cpp.

97 {
98 // Create the stream we will serialize into.
100
101 // Create the image sharing proc.
103 SkSerialProcs procs;
105 procs.fImageCtx = &ctx;
106
107 // Create the multi picture document used for recording frames.
109
110 static const int NUM_FRAMES = 12;
111 static const int WIDTH = 256;
112 static const int HEIGHT = 256;
113
114 // Make an image to be used in a later step.
116 surface->getCanvas()->clear(SK_ColorGREEN);
117 sk_sp<SkImage> image(surface->makeImageSnapshot());
119
120 // Make a subpicture to be used in a later step
122 SkCanvas* subCanvas = pr.beginRecording(100, 100);
123 draw_basic(subCanvas, 42, image);
125
127 std::vector<sk_sp<SkImage>> expectedImages;
128
129 for (int i=0; i<NUM_FRAMES; i++) {
130 SkCanvas* pictureCanvas = multipic->beginPage(WIDTH, HEIGHT);
131 draw_advanced(pictureCanvas, i, image, sub);
132 multipic->endPage();
133 // Also draw the picture to an image for later comparison
134 auto surf = SkSurfaces::Raster(info);
135 draw_advanced(surf->getCanvas(), i, image, sub);
136 expectedImages.push_back(surf->makeImageSnapshot());
137 }
138 // Finalize
139 multipic->close();
140
141 // Confirm written data is at least as large as the magic word
142 std::unique_ptr<SkStreamAsset> writtenStream = stream.detachAsStream();
143 REPORTER_ASSERT(reporter, writtenStream->getLength() > 24,
144 "Written data length too short (%zu)", writtenStream->getLength());
145 // SkDebugf("Multi Frame file size = %zu\n", writtenStream->getLength());
146
147 // Set up deserialization
148 SkSharingDeserialContext deserialContext;
149 SkDeserialProcs dprocs;
151 dprocs.fImageCtx = &deserialContext;
152
153 // Confirm data is a MultiPictureDocument
154 int frame_count = SkMultiPictureDocument::ReadPageCount(writtenStream.get());
155 REPORTER_ASSERT(reporter, frame_count == NUM_FRAMES,
156 "Expected %d frames, got %d. \n 0 frames may indicate the written file was not a "
157 "MultiPictureDocument.", NUM_FRAMES, frame_count);
158
159 // Deserialize
160 std::vector<SkDocumentPage> frames(frame_count);
162 SkMultiPictureDocument::Read(writtenStream.get(), frames.data(), frame_count, &dprocs),
163 "Failed while reading MultiPictureDocument");
164
165 // Examine each frame.
166 int i=0;
167 for (const auto& frame : frames) {
168 SkRect bounds = frame.fPicture->cullRect();
170 "Page width: expected (%d) got (%d)", WIDTH, (int)bounds.width());
172 "Page height: expected (%d) got (%d)", HEIGHT, (int)bounds.height());
173
174 auto surf = SkSurfaces::Raster(info);
175 surf->getCanvas()->drawPicture(frame.fPicture);
176 auto img = surf->makeImageSnapshot();
177 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img.get(), expectedImages[i].get()),
178 "Frame %d is wrong", i);
179
180 i++;
181 }
182}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
reporter
Definition: FontMgrTest.cpp:39
static void draw_advanced(SkCanvas *canvas, int seed, sk_sp< SkImage > image, sk_sp< SkPicture > sub)
static void draw_basic(SkCanvas *canvas, int seed, sk_sp< SkImage > image)
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define WIDTH
#define HEIGHT
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
VkSurfaceKHR surface
Definition: main.cc:49
double frame
Definition: examples.cpp:31
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
SK_API bool Read(SkStreamSeekable *src, SkDocumentPage *dstArray, int dstArrayCount, const SkDeserialProcs *=nullptr)
SK_API int ReadPageCount(SkStreamSeekable *src)
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
bool equal_pixels(const SkPixmap &a, const SkPixmap &b)
Definition: ToolUtils.cpp:456
SkDeserialImageProc fImageProc
static SkImageInfo MakeN32Premul(int width, int height)
void * fImageCtx
Definition: SkSerialProcs.h:91
SkSerialImageProc fImageProc
Definition: SkSerialProcs.h:90
static sk_sp< SkImage > deserializeImage(const void *data, size_t length, void *ctx)
static sk_sp< SkData > serializeImage(SkImage *img, void *ctx)

◆ draw_advanced()

static void draw_advanced ( SkCanvas canvas,
int  seed,
sk_sp< SkImage image,
sk_sp< SkPicture sub 
)
static

Definition at line 85 of file MultiPictureDocumentTest.cpp.

85 {
86 draw_basic(canvas, seed, image);
87
88 // Use subpicture twice in different places
89 canvas->drawPicture(sub);
90 canvas->save();
91 canvas->translate(seed, seed);
92 canvas->drawPicture(sub);
93 canvas->restore();
94}
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
void drawPicture(const SkPicture *picture)
Definition: SkCanvas.h:1961

◆ draw_basic()

static void draw_basic ( SkCanvas canvas,
int  seed,
sk_sp< SkImage image 
)
static

Definition at line 41 of file MultiPictureDocumentTest.cpp.

41 {
42 canvas->drawColor(SK_ColorWHITE);
43
46 paint.setStrokeWidth(seed);
47 paint.setColor(SK_ColorRED);
48
49 SkRect rect = SkRect::MakeXYWH(50+seed, 50+seed, 4*seed, 60);
50 canvas->drawRect(rect, paint);
51
53 oval.setOval(rect);
54 oval.offset(40, 60+seed);
55 paint.setColor(SK_ColorBLUE);
56 canvas->drawRRect(oval, paint);
57
58 paint.setColor(SK_ColorCYAN);
59 canvas->drawCircle(180, 50, 5*seed, paint);
60
61 rect.offset(80, 0);
62 paint.setColor(SK_ColorYELLOW);
63 canvas->drawRoundRect(rect, 10, 10, paint);
64
66 path.cubicTo(768, 0, -512, 256, 256, 256);
67 paint.setColor(SK_ColorGREEN);
68 canvas->drawPath(path, paint);
69
70 canvas->drawImage(image, 128-seed, 128, SkSamplingOptions(), &paint);
71
72 if (seed % 2 == 0) {
73 SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60);
74 canvas->drawImageRect(image, rect2, SkSamplingOptions(), &paint);
75 }
76
77 SkPaint paint2;
79 font.setSize(2 + seed);
80 auto text = SkTextBlob::MakeFromString(SkStringPrintf("Frame %d", seed).c_str(), font);
81 canvas->drawTextBlob(text.get(), 50, 25, paint2);
82}
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
Definition: SkCanvas.cpp:1705
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
Definition: SkCanvas.cpp:2717
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
Definition: SkFont.h:35
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
Definition: SkPath.h:59
static sk_sp< SkTextBlob > MakeFromString(const char *string, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: SkTextBlob.h:115
const Paint & paint
Definition: color_source.cc:38
std::u16string text
SkRect oval
Definition: SkRecords.h:249
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkFont DefaultFont()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
void offset(float dx, float dy)
Definition: SkRect.h:1016