Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | * |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 7 | * This test confirms that a MultiPictureDocument can be serialized and deserailzied without error. |
| 8 | * And that the pictures within it are re-created accurately |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 12 | #include "include/core/SkColorSpace.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 13 | #include "include/core/SkDocument.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 14 | #include "include/core/SkFont.h" |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 15 | #include "include/core/SkImage.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 16 | #include "include/core/SkPicture.h" |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 17 | #include "include/core/SkPictureRecorder.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkSurface.h" |
| 20 | #include "include/core/SkTextBlob.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 21 | #include "src/utils/SkMultiPictureDocument.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 22 | #include "tests/Test.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 23 | #include "tools/SkSharingProc.h" |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 24 | #include "tools/ToolUtils.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 25 | |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 26 | // Covers rects, ovals, paths, images, text |
| 27 | static void draw_basic(SkCanvas* canvas, int seed, sk_sp<SkImage> image) { |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 28 | canvas->drawColor(SK_ColorWHITE); |
| 29 | |
| 30 | SkPaint paint; |
| 31 | paint.setStyle(SkPaint::kStroke_Style); |
| 32 | paint.setStrokeWidth(seed); |
| 33 | paint.setColor(SK_ColorRED); |
| 34 | |
| 35 | SkRect rect = SkRect::MakeXYWH(50+seed, 50+seed, 4*seed, 60); |
| 36 | canvas->drawRect(rect, paint); |
| 37 | |
| 38 | SkRRect oval; |
| 39 | oval.setOval(rect); |
| 40 | oval.offset(40, 60+seed); |
| 41 | paint.setColor(SK_ColorBLUE); |
| 42 | canvas->drawRRect(oval, paint); |
| 43 | |
| 44 | paint.setColor(SK_ColorCYAN); |
| 45 | canvas->drawCircle(180, 50, 5*seed, paint); |
| 46 | |
| 47 | rect.offset(80, 0); |
| 48 | paint.setColor(SK_ColorYELLOW); |
| 49 | canvas->drawRoundRect(rect, 10, 10, paint); |
| 50 | |
| 51 | SkPath path; |
| 52 | path.cubicTo(768, 0, -512, 256, 256, 256); |
| 53 | paint.setColor(SK_ColorGREEN); |
| 54 | canvas->drawPath(path, paint); |
| 55 | |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 56 | canvas->drawImage(image, 128-seed, 128, SkSamplingOptions(), &paint); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 57 | |
| 58 | if (seed % 2 == 0) { |
| 59 | SkRect rect2 = SkRect::MakeXYWH(0, 0, 40, 60); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 60 | canvas->drawImageRect(image, rect2, SkSamplingOptions(), &paint); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | SkPaint paint2; |
| 64 | auto text = SkTextBlob::MakeFromString( |
| 65 | SkStringPrintf("Frame %d", seed).c_str(), SkFont(nullptr, 2+seed)); |
| 66 | canvas->drawTextBlob(text.get(), 50, 25, paint2); |
| 67 | } |
| 68 | |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 69 | // Covers all of the above and drawing nested sub-pictures. |
| 70 | static void draw_advanced(SkCanvas* canvas, int seed, sk_sp<SkImage> image, sk_sp<SkPicture> sub) { |
| 71 | draw_basic(canvas, seed, image); |
| 72 | |
| 73 | // Use subpicture twice in different places |
| 74 | canvas->drawPicture(sub); |
| 75 | canvas->save(); |
| 76 | canvas->translate(seed, seed); |
| 77 | canvas->drawPicture(sub); |
| 78 | canvas->restore(); |
| 79 | } |
| 80 | |
| 81 | // Test serialization and deserialization of multi picture document |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 82 | DEF_TEST(SkMultiPictureDocument_Serialize_and_deserialize, reporter) { |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 83 | // Create the stream we will serialize into. |
| 84 | SkDynamicMemoryWStream stream; |
| 85 | |
| 86 | // Create the image sharing proc. |
| 87 | SkSharingSerialContext ctx; |
| 88 | SkSerialProcs procs; |
| 89 | procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 90 | procs.fImageCtx = &ctx; |
| 91 | |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 92 | // Create the multi picture document used for recording frames. |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 93 | sk_sp<SkDocument> multipic = SkMakeMultiPictureDocument(&stream, &procs); |
| 94 | |
| 95 | static const int NUM_FRAMES = 12; |
| 96 | static const int WIDTH = 256; |
| 97 | static const int HEIGHT = 256; |
| 98 | |
| 99 | // Make an image to be used in a later step. |
| 100 | auto surface(SkSurface::MakeRasterN32Premul(100, 100)); |
| 101 | surface->getCanvas()->clear(SK_ColorGREEN); |
| 102 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
| 103 | REPORTER_ASSERT(reporter, image); |
| 104 | |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 105 | // Make a subpicture to be used in a later step |
| 106 | SkPictureRecorder pr; |
| 107 | SkCanvas* subCanvas = pr.beginRecording(100, 100); |
| 108 | draw_basic(subCanvas, 42, image); |
| 109 | sk_sp<SkPicture> sub = pr.finishRecordingAsPicture(); |
| 110 | |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 111 | const SkImageInfo info = SkImageInfo::MakeN32Premul(WIDTH, HEIGHT); |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 112 | std::vector<sk_sp<SkImage>> expectedImages; |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 113 | |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 114 | for (int i=0; i<NUM_FRAMES; i++) { |
| 115 | SkCanvas* pictureCanvas = multipic->beginPage(WIDTH, HEIGHT); |
Nathaniel Nifong | 7ee3f93 | 2019-09-25 14:52:44 -0400 | [diff] [blame] | 116 | draw_advanced(pictureCanvas, i, image, sub); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 117 | multipic->endPage(); |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 118 | // Also draw the picture to an image for later comparison |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 119 | auto surf = SkSurface::MakeRaster(info); |
| 120 | draw_advanced(surf->getCanvas(), i, image, sub); |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 121 | expectedImages.push_back(surf->makeImageSnapshot()); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 122 | } |
| 123 | // Finalize |
| 124 | multipic->close(); |
| 125 | |
| 126 | // Confirm written data is at least as large as the magic word |
| 127 | std::unique_ptr<SkStreamAsset> writtenStream = stream.detachAsStream(); |
| 128 | REPORTER_ASSERT(reporter, writtenStream->getLength() > 24, |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 129 | "Written data length too short (%zu)", writtenStream->getLength()); |
| 130 | // SkDebugf("Multi Frame file size = %zu\n", writtenStream->getLength()); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 131 | |
| 132 | // Set up deserialization |
| 133 | SkSharingDeserialContext deserialContext; |
| 134 | SkDeserialProcs dprocs; |
| 135 | dprocs.fImageProc = SkSharingDeserialContext::deserializeImage; |
| 136 | dprocs.fImageCtx = &deserialContext; |
| 137 | |
| 138 | // Confirm data is a MultiPictureDocument |
| 139 | int frame_count = SkMultiPictureDocumentReadPageCount(writtenStream.get()); |
| 140 | REPORTER_ASSERT(reporter, frame_count == NUM_FRAMES, |
| 141 | "Expected %d frames, got %d. \n 0 frames may indicate the written file was not a " |
| 142 | "MultiPictureDocument.", NUM_FRAMES, frame_count); |
| 143 | |
| 144 | // Deserailize |
| 145 | std::vector<SkDocumentPage> frames(frame_count); |
| 146 | REPORTER_ASSERT(reporter, |
| 147 | SkMultiPictureDocumentRead(writtenStream.get(), frames.data(), frame_count, &dprocs), |
| 148 | "Failed while reading MultiPictureDocument"); |
| 149 | |
| 150 | // Examine each frame. |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 151 | int i=0; |
| 152 | for (const auto& frame : frames) { |
| 153 | SkRect bounds = frame.fPicture->cullRect(); |
| 154 | REPORTER_ASSERT(reporter, bounds.width() == WIDTH, |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 155 | "Page width: expected (%d) got (%d)", WIDTH, (int)bounds.width()); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, bounds.height() == HEIGHT, |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 157 | "Page height: expected (%d) got (%d)", HEIGHT, (int)bounds.height()); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 158 | |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 159 | auto surf = SkSurface::MakeRaster(info); |
| 160 | surf->getCanvas()->drawPicture(frame.fPicture); |
| 161 | auto img = surf->makeImageSnapshot(); |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img.get(), expectedImages[i].get())); |
Mike Reed | 47d795a | 2020-08-24 13:21:51 -0400 | [diff] [blame] | 163 | |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 164 | i++; |
| 165 | } |
| 166 | } |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 167 | |
| 168 | |
| 169 | #if SK_SUPPORT_GPU && defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
| 170 | |
| 171 | #include "include/gpu/GrDirectContext.h" |
| 172 | #include "src/gpu/GrAHardwareBufferUtils.h" |
| 173 | #include "src/gpu/GrDirectContextPriv.h" |
| 174 | |
| 175 | #include <android/hardware_buffer.h> |
| 176 | |
| 177 | static const int DEV_W = 16, DEV_H = 16; |
| 178 | |
| 179 | static SkPMColor get_src_color(int x, int y) { |
| 180 | SkASSERT(x >= 0 && x < DEV_W); |
| 181 | SkASSERT(y >= 0 && y < DEV_H); |
| 182 | |
| 183 | U8CPU r = x; |
| 184 | U8CPU g = y; |
| 185 | U8CPU b = 0xc; |
| 186 | |
| 187 | U8CPU a = 0xff; |
| 188 | switch ((x+y) % 5) { |
| 189 | case 0: |
| 190 | a = 0xff; |
| 191 | break; |
| 192 | case 1: |
| 193 | a = 0x80; |
| 194 | break; |
| 195 | case 2: |
| 196 | a = 0xCC; |
| 197 | break; |
| 198 | case 4: |
| 199 | a = 0x01; |
| 200 | break; |
| 201 | case 3: |
| 202 | a = 0x00; |
| 203 | break; |
| 204 | } |
| 205 | a = 0xff; |
| 206 | return SkPremultiplyARGBInline(a, r, g, b); |
| 207 | } |
| 208 | |
| 209 | static SkBitmap make_src_bitmap() { |
| 210 | static SkBitmap bmp; |
| 211 | if (bmp.isNull()) { |
| 212 | bmp.allocN32Pixels(DEV_W, DEV_H); |
| 213 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 214 | for (int y = 0; y < DEV_H; ++y) { |
| 215 | for (int x = 0; x < DEV_W; ++x) { |
| 216 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>( |
| 217 | pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
| 218 | *pixel = get_src_color(x, y); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | return bmp; |
| 223 | } |
| 224 | |
| 225 | static void cleanup_resources(AHardwareBuffer* buffer) { |
| 226 | if (buffer) { |
| 227 | AHardwareBuffer_release(buffer); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static sk_sp<SkImage> makeAHardwareBufferTestImage( |
| 232 | skiatest::Reporter* reporter, GrDirectContext* context, AHardwareBuffer* buffer) { |
| 233 | |
| 234 | const SkBitmap srcBitmap = make_src_bitmap(); |
| 235 | |
| 236 | AHardwareBuffer_Desc hwbDesc; |
| 237 | hwbDesc.width = DEV_W; |
| 238 | hwbDesc.height = DEV_H; |
| 239 | hwbDesc.layers = 1; |
| 240 | hwbDesc.usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | |
| 241 | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | |
| 242 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE; |
| 243 | hwbDesc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
| 244 | // The following three are not used in the allocate |
| 245 | hwbDesc.stride = 0; |
| 246 | hwbDesc.rfu0= 0; |
| 247 | hwbDesc.rfu1= 0; |
| 248 | |
| 249 | if (int error = AHardwareBuffer_allocate(&hwbDesc, &buffer)) { |
| 250 | ERRORF(reporter, "Failed to allocated hardware buffer, error: %d", error); |
| 251 | cleanup_resources(buffer); |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
| 255 | // Get actual desc for allocated buffer so we know the stride for uploading cpu data. |
| 256 | AHardwareBuffer_describe(buffer, &hwbDesc); |
| 257 | |
| 258 | void* bufferAddr; |
| 259 | if (AHardwareBuffer_lock(buffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, nullptr, |
| 260 | &bufferAddr)) { |
| 261 | ERRORF(reporter, "Failed to lock hardware buffer"); |
| 262 | cleanup_resources(buffer); |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
| 266 | // fill buffer |
| 267 | int bbp = srcBitmap.bytesPerPixel(); |
| 268 | uint32_t* src = (uint32_t*)srcBitmap.getPixels(); |
| 269 | int nextLineStep = DEV_W; |
| 270 | uint32_t* dst = static_cast<uint32_t*>(bufferAddr); |
| 271 | for (int y = 0; y < DEV_H; ++y) { |
| 272 | memcpy(dst, src, DEV_W * bbp); |
| 273 | src += nextLineStep; |
| 274 | dst += hwbDesc.stride; |
| 275 | } |
| 276 | AHardwareBuffer_unlock(buffer, nullptr); |
| 277 | |
| 278 | // Make SkImage from buffer in a way that mimics libs/hwui/AutoBackendTextureRelease |
| 279 | GrBackendFormat backendFormat = |
| 280 | GrAHardwareBufferUtils::GetBackendFormat(context, buffer, hwbDesc.format, false); |
| 281 | GrAHardwareBufferUtils::DeleteImageProc deleteProc; |
| 282 | GrAHardwareBufferUtils::UpdateImageProc updateProc; |
| 283 | GrAHardwareBufferUtils::TexImageCtx imageCtx; |
| 284 | GrBackendTexture texture = GrAHardwareBufferUtils::MakeBackendTexture( |
| 285 | context, buffer, hwbDesc.width, hwbDesc.height, |
| 286 | &deleteProc, // set by MakeBackendTexture |
| 287 | &updateProc, // set by MakeBackendTexture |
| 288 | &imageCtx, // set by MakeBackendTexture |
| 289 | false, // don't make protected image |
| 290 | backendFormat, |
| 291 | false // isRenderable |
| 292 | ); |
| 293 | SkColorType colorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(hwbDesc.format); |
| 294 | sk_sp<SkImage> image = SkImage::MakeFromTexture( |
| 295 | context, texture, kTopLeft_GrSurfaceOrigin, colorType, kPremul_SkAlphaType, |
| 296 | SkColorSpace::MakeSRGB(), |
| 297 | nullptr, // no release proc |
| 298 | nullptr // context for release proc |
| 299 | ); |
| 300 | |
| 301 | REPORTER_ASSERT(reporter, image); |
| 302 | REPORTER_ASSERT(reporter, image->isTextureBacked()); |
| 303 | return image; |
| 304 | } |
| 305 | |
| 306 | // Test the onEndPage callback's intended use by processing an mskp containing AHardwareBuffer-backed SkImages |
| 307 | // Expected behavior is that the callback is called while the AHardwareBuffer is still valid and the |
| 308 | // images are copied so .close() can still access them. |
| 309 | // Confirm deserialized file contains images with correct data. |
| 310 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkMultiPictureDocument_AHardwarebuffer, |
| 311 | reporter, ctx_info) { |
| 312 | auto context = ctx_info.directContext(); |
| 313 | if (!context->priv().caps()->supportsAHardwareBufferImages()) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | // Create the stream we will serialize into. |
| 318 | SkDynamicMemoryWStream stream; |
| 319 | |
| 320 | // Create the image sharing proc. |
| 321 | SkSharingSerialContext ctx; |
| 322 | SkSerialProcs procs; |
| 323 | procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 324 | procs.fImageCtx = &ctx; |
| 325 | |
| 326 | // Create the multi picture document used for recording frames. |
| 327 | // Pass a lambda as the onEndPage callback that captures our sharing context |
| 328 | sk_sp<SkDocument> multipic = SkMakeMultiPictureDocument(&stream, &procs, |
| 329 | [sharingCtx = &ctx](const SkPicture* pic) { |
| 330 | SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx); |
| 331 | }); |
| 332 | |
| 333 | static const int WIDTH = 256; |
| 334 | static const int HEIGHT = 256; |
| 335 | |
| 336 | // Make an image to be used in a later step. |
| 337 | AHardwareBuffer* ahbuffer = nullptr; |
| 338 | sk_sp<SkImage> image = makeAHardwareBufferTestImage(reporter, context, ahbuffer); |
| 339 | |
| 340 | const SkImageInfo info = SkImageInfo::MakeN32Premul(WIDTH, HEIGHT); |
| 341 | std::vector<sk_sp<SkImage>> expectedImages; |
| 342 | |
| 343 | // Record single frame |
| 344 | SkCanvas* pictureCanvas = multipic->beginPage(WIDTH, HEIGHT); |
| 345 | draw_basic(pictureCanvas, 0, image); |
| 346 | multipic->endPage(); |
| 347 | // Also draw the picture to an image for later comparison |
| 348 | auto surf = SkSurface::MakeRaster(info); |
| 349 | draw_basic(surf->getCanvas(), 0, image); |
| 350 | expectedImages.push_back(surf->makeImageSnapshot()); |
| 351 | |
| 352 | // Release Ahardwarebuffer. If the code under test has not copied it already, |
| 353 | // close() will fail. |
| 354 | // Note that this only works because we're doing one frame only. If this test were recording |
| 355 | // two or more frames, it would have change the buffer contents instead. |
| 356 | cleanup_resources(ahbuffer); |
| 357 | |
| 358 | // Finalize |
| 359 | multipic->close(); |
| 360 | |
| 361 | // Confirm written data is at least as large as the magic word |
| 362 | std::unique_ptr<SkStreamAsset> writtenStream = stream.detachAsStream(); |
| 363 | REPORTER_ASSERT(reporter, writtenStream->getLength() > 24, |
| 364 | "Written data length too short (%zu)", writtenStream->getLength()); |
| 365 | |
| 366 | // Set up deserialization |
| 367 | SkSharingDeserialContext deserialContext; |
| 368 | SkDeserialProcs dprocs; |
| 369 | dprocs.fImageProc = SkSharingDeserialContext::deserializeImage; |
| 370 | dprocs.fImageCtx = &deserialContext; |
| 371 | |
| 372 | // Confirm data is a MultiPictureDocument |
| 373 | int frame_count = SkMultiPictureDocumentReadPageCount(writtenStream.get()); |
| 374 | REPORTER_ASSERT(reporter, frame_count == 1, |
| 375 | "Expected 1 frame, got %d. \n 0 frames may indicate the written file was not a " |
| 376 | "MultiPictureDocument.", frame_count); |
| 377 | |
| 378 | // Deserialize |
| 379 | std::vector<SkDocumentPage> frames(frame_count); |
| 380 | REPORTER_ASSERT(reporter, |
| 381 | SkMultiPictureDocumentRead(writtenStream.get(), frames.data(), frame_count, &dprocs), |
| 382 | "Failed while reading MultiPictureDocument"); |
| 383 | |
| 384 | // Examine frame. |
| 385 | SkRect bounds = frames[0].fPicture->cullRect(); |
| 386 | REPORTER_ASSERT(reporter, bounds.width() == WIDTH, |
| 387 | "Page width: expected (%d) got (%d)", WIDTH, (int)bounds.width()); |
| 388 | REPORTER_ASSERT(reporter, bounds.height() == HEIGHT, |
| 389 | "Page height: expected (%d) got (%d)", HEIGHT, (int)bounds.height()); |
| 390 | |
| 391 | auto surf2 = SkSurface::MakeRaster(info); |
| 392 | surf2->getCanvas()->drawPicture(frames[0].fPicture); |
| 393 | auto img = surf2->makeImageSnapshot(); |
| 394 | REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img.get(), expectedImages[0].get())); |
| 395 | } |
| 396 | |
| 397 | #endif // android compilation |