static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { // Create a bitmap that will be encoded. SkBitmap original; make_bm(&original, 100, 100, SK_ColorBLUE, true); SkDynamicMemoryWStream wStream; if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { return; } SkAutoDataUnref data(wStream.copyToData()); SkBitmap bm; bool installSuccess = SkDecodingImageGenerator::Install(data, &bm); REPORTER_ASSERT(reporter, installSuccess); // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. // Flattening original will follow the old path of performing an encode, while flattening bm // will use the already encoded data. SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); REPORTER_ASSERT(reporter, picture1->equals(picture2)); // Now test that a parse error was generated when trying to create a new SkPicture without // providing a function to decode the bitmap. ErrorContext context; context.fErrors = 0; context.fReporter = reporter; SkSetErrorCallback(assert_one_parse_error_cb, &context); SkMemoryStream pictureStream(picture1); SkClearLastError(); SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); SkClearLastError(); SkSetErrorCallback(NULL, NULL); }
DEF_TEST(Picture_EncodedData, reporter) { // Create a bitmap that will be encoded. SkBitmap original; make_bm(&original, 100, 100, SK_ColorBLUE, true); SkDynamicMemoryWStream wStream; if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { return; } SkAutoDataUnref data(wStream.copyToData()); SkBitmap bm; bool installSuccess = SkDEPRECATED_InstallDiscardablePixelRef(data, &bm); REPORTER_ASSERT(reporter, installSuccess); // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. // Flattening original will follow the old path of performing an encode, while flattening bm // will use the already encoded data. SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); REPORTER_ASSERT(reporter, picture1->equals(picture2)); // Now test that a parse error was generated when trying to create a new SkPicture without // providing a function to decode the bitmap. ErrorContext context; context.fErrors = 0; context.fReporter = reporter; SkSetErrorCallback(assert_one_parse_error_cb, &context); SkMemoryStream pictureStream(picture1); SkClearLastError(); sk_sp<SkPicture> pictureFromStream(SkPicture::MakeFromStream(&pictureStream, nullptr)); REPORTER_ASSERT(reporter, pictureFromStream.get() != nullptr); SkClearLastError(); SkSetErrorCallback(nullptr, nullptr); // Test that using the version of CreateFromStream that just takes a stream also decodes the // bitmap. Drawing this picture should look exactly like the original bitmap. SkMD5::Digest referenceDigest; md5(original, &referenceDigest); SkBitmap dst; dst.allocPixels(original.info()); dst.eraseColor(SK_ColorRED); SkCanvas canvas(dst); pictureStream.rewind(); pictureFromStream = SkPicture::MakeFromStream(&pictureStream); canvas.drawPicture(pictureFromStream.get()); SkMD5::Digest digest2; md5(dst, &digest2); REPORTER_ASSERT(reporter, referenceDigest == digest2); }
virtual void HandleMessage(const pp::Var& var_message) { // Receive a message from javascript. if (var_message.is_string()) { SkString msg(var_message.AsString().c_str()); if (msg.startsWith("init")) { } else if (msg.startsWith("LoadSKP")) { size_t startIndex = strlen("LoadSKP"); size_t dataSize = msg.size()/sizeof(char) - startIndex; SkBase64 decodedData; decodedData.decode(msg.c_str() + startIndex, dataSize); size_t decodedSize = 3 * (dataSize / 4); SkDebugf("Got size: %d\n", decodedSize); if (!decodedData.getData()) { SkDebugf("Failed to decode SKP\n"); return; } SkMemoryStream pictureStream(decodedData.getData(), decodedSize); SkPicture* picture = SkPicture::CreateFromStream(&pictureStream); if (NULL == picture) { SkDebugf("Failed to create SKP.\n"); return; } fDebugger.loadPicture(picture); picture->unref(); // Set up the command list. SkTArray<SkString>* commands = fDebugger.getDrawCommandsAsStrings(); PostMessage("ClearCommands"); for (int i = 0; i < commands->count(); ++i) { SkString addCommand("AddCommand:"); addCommand.append((*commands)[i]); PostMessage(addCommand.c_str()); } PostMessage("UpdateCommands"); // Set the overview text. SkString overviewText; fDebugger.getOverviewText(NULL, 0.0, &overviewText, 1); overviewText.prepend("SetOverview:"); PostMessage(overviewText.c_str()); // Draw the SKP. if (!fFlushLoopRunning) { Paint(); } } else if (msg.startsWith("CommandSelected:")) { size_t startIndex = strlen("CommandSelected:"); int index = atoi(msg.c_str() + startIndex); fDebugger.setIndex(index); if (!fFlushLoopRunning) { Paint(); } } else if (msg.startsWith("Rewind")) { fCanvas->clear(SK_ColorWHITE); fDebugger.setIndex(0); if (!fFlushLoopRunning) { Paint(); } } else if (msg.startsWith("StepBack")) { fCanvas->clear(SK_ColorWHITE); int currentIndex = fDebugger.index(); if (currentIndex > 1) { fDebugger.setIndex(currentIndex - 1); if (!fFlushLoopRunning) { Paint(); } } } else if (msg.startsWith("Pause")) { // TODO(borenet) } else if (msg.startsWith("StepForward")) { int currentIndex = fDebugger.index(); if (currentIndex < fDebugger.getSize() -1) { fDebugger.setIndex(currentIndex + 1); if (!fFlushLoopRunning) { Paint(); } } } else if (msg.startsWith("Play")) { fDebugger.setIndex(fDebugger.getSize() - 1); if (!fFlushLoopRunning) { Paint(); } } } }