static void testOne(const SkString& filename) { #if DEBUG_SHOW_TEST_NAME SkString testName(filename); const char http[] = "http"; if (testName.startsWith(http)) { testName.remove(0, sizeof(http) - 1); } while (testName.startsWith("_")) { testName.remove(0, 1); } const char dotSkp[] = ".skp"; if (testName.endsWith(dotSkp)) { size_t len = testName.size(); testName.remove(len - (sizeof(dotSkp) - 1), sizeof(dotSkp) - 1); } testName.prepend("skp"); testName.append("1"); strncpy(DEBUG_FILENAME_STRING, testName.c_str(), DEBUG_FILENAME_STRING_LENGTH); #endif SkString path; make_filepath(&path, pictDir, filename); SkFILEStream stream(path.c_str()); if (!stream.isValid()) { return; } SkPicture* pic = SkPicture::CreateFromStream(&stream, &SkImageDecoder::DecodeMemory); if (!pic) { SkDebugf("unable to decode %s\n", filename.c_str()); return; } int width = pic->width(); int height = pic->height(); SkBitmap bitmap; bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); bool success = bitmap.allocPixels(); if (!success) { SkDebugf("unable to allocate bitmap for %s\n", filename.c_str()); return; } SkCanvas canvas(bitmap); SkString pngName(filename); pngName.remove(pngName.size() - 3, 3); pngName.append("png"); for (int i = 0; i < 2; ++i) { bool useOp = i ? true : false; canvas.setAllowSimplifyClip(useOp); pic->draw(&canvas); SkString outFile; make_filepath(&outFile, useOp ? outSkpClipDir : outOldClipDir, pngName); SkImageEncoder::EncodeFile(outFile.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); } SkDELETE(pic); }
static void writePict(const SkBitmap& bitmap, const char* outDir, const char* pngName) { SkString outFile = make_filepath(0, outDir, pngName); if (!sk_tool_utils::EncodeImageToFile(outFile.c_str(), bitmap, SkEncodedImageFormat::kPNG, 100)) { SkDebugf("unable to encode gr %s (width=%d height=%d)br \n", pngName, bitmap.width(), bitmap.height()); } }
static void writePict(const SkBitmap& bitmap, const char* outDir, const char* pngName) { SkString outFile = make_filepath(0, outDir, pngName); if (!SkImageEncoder::EncodeFile(outFile.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100)) { SkDebugf("unable to encode gr %s (width=%d height=%d)br \n", pngName, bitmap.width(), bitmap.height()); } }
bool CopyTilesRenderer::render(SkBitmap** out) { int i = 0; bool success = true; SkBitmap dst; for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) { for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) { SkAutoCanvasRestore autoRestore(fCanvas, true); // Translate so that we draw the correct portion of the picture. // Perform a postTranslate so that the scaleFactor does not interfere with the // positioning. SkMatrix mat(fCanvas->getTotalMatrix()); mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); fCanvas->setMatrix(mat); // Draw the picture fCanvas->drawPicture(*fPicture); // Now extract the picture into tiles const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false); SkIRect subset; for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) { for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) { subset.set(tileX, tileY, tileX + this->getTileWidth(), tileY + this->getTileHeight()); SkDEBUGCODE(bool extracted =) baseBitmap.extractSubset(&dst, subset); SkASSERT(extracted); if (!fOutputDir.isEmpty()) { // Similar to write() in PictureRenderer.cpp, but just encodes // a bitmap directly. // TODO: Share more common code with write() to do this, to properly // write out the JSON summary, etc. SkString pathWithNumber; make_filepath(&pathWithNumber, fOutputDir, fInputFilename); pathWithNumber.remove(pathWithNumber.size() - 4, 4); pathWithNumber.appendf("%i.png", i++); SkBitmap copy; #if SK_SUPPORT_GPU if (isUsingGpuDevice()) { dst.pixelRef()->readPixels(©, &subset); } else { #endif dst.copyTo(©); #if SK_SUPPORT_GPU } #endif success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy, SkImageEncoder::kPNG_Type, 100); } } } } } return success; }
static bool make_out_dirs() { SkString outDir = make_filepath(0, OUT_DIR, ""); if (!sk_exists(outDir.c_str())) { if (!sk_mkdir(outDir.c_str())) { SkDebugf("could not create dir %s\n", outDir.c_str()); return false; } } SkString grDir = make_filepath(0, outGrDir, ""); if (!sk_exists(grDir.c_str())) { if (!sk_mkdir(grDir.c_str())) { SkDebugf("could not create dir %s\n", grDir.c_str()); return false; } } SkString skDir = make_filepath(0, outSkDir, ""); if (!sk_exists(skDir.c_str())) { if (!sk_mkdir(skDir.c_str())) { SkDebugf("could not create dir %s\n", skDir.c_str()); return false; } } SkString skpDir = make_filepath(0, outSkpDir, ""); if (!sk_exists(skpDir.c_str())) { if (!sk_mkdir(skpDir.c_str())) { SkDebugf("could not create dir %s\n", skpDir.c_str()); return false; } } SkString diffDir = make_filepath(0, outDiffDir, ""); if (!sk_exists(diffDir.c_str())) { if (!sk_mkdir(diffDir.c_str())) { SkDebugf("could not create dir %s\n", diffDir.c_str()); return false; } } SkString statusDir = make_filepath(0, outStatusDir, ""); if (!sk_exists(statusDir.c_str())) { if (!sk_mkdir(statusDir.c_str())) { SkDebugf("could not create dir %s\n", statusDir.c_str()); return false; } } return true; }
DEF_TEST(SkpSkGrThreaded, reporter) { if (!initTest()) { return; } SkpSkGrThreadedTestRunner testRunner(reporter); for (int dirIndex = 1; dirIndex <= 100; ++dirIndex) { SkString pictDir = make_in_dir_name(dirIndex); if (pictDir.size() == 0) { continue; } SkOSFile::Iter iter(pictDir.c_str(), "skp"); SkString filename; while (iter.next(&filename)) { SkString pngName = make_png_name(filename.c_str()); SkString oldPng = make_filepath(dirIndex, outSkDir, pngName.c_str()); SkString newPng = make_filepath(dirIndex, outGrDir, pngName.c_str()); if (sk_exists(oldPng.c_str()) && sk_exists(newPng.c_str())) { bumpCount(reporter, true); continue; } for (size_t index = 0; index < skipOverSkGrCount; ++index) { if (skipOverSkGr[index].directory == dirIndex && strcmp(filename.c_str(), skipOverSkGr[index].filename) == 0) { bumpCount(reporter, true); goto skipOver; } } *testRunner.fRunnables.append() = new SkpSkGrThreadedRunnable( &testSkGrMain, dirIndex, filename.c_str(), &testRunner); skipOver: ; } } testRunner.render(); SkpSkGrThreadState& max = testRunner.fRunnables[0]->fState; for (int dirIndex = 2; dirIndex <= 100; ++dirIndex) { SkpSkGrThreadState& state = testRunner.fRunnables[dirIndex - 1]->fState; for (int index = 0; index < state.fFoundCount; ++index) { int maxIdx = max.fFoundCount; if (maxIdx < kMaxFiles) { max.fError[maxIdx] = state.fError[index]; strcpy(max.fFilesFound[maxIdx], state.fFilesFound[index]); max.fDirsFound[maxIdx] = state.fDirsFound[index]; ++max.fFoundCount; continue; } for (maxIdx = 0; maxIdx < max.fFoundCount; ++maxIdx) { if (max.fError[maxIdx] < state.fError[index]) { max.fError[maxIdx] = state.fError[index]; strcpy(max.fFilesFound[maxIdx], state.fFilesFound[index]); max.fDirsFound[maxIdx] = state.fDirsFound[index]; break; } } } } TestResult encoder; encoder.fTestStep = kEncodeFiles; for (int index = 0; index < max.fFoundCount; ++index) { encoder.fDirNo = max.fDirsFound[index]; strcpy(encoder.fFilename, max.fFilesFound[index]); encoder.testOne(); SkDebugf("+"); } }
static SkString makeStatusString(int dirNo) { SkString statName; statName.printf("stats%d.txt", dirNo); SkString statusFile = make_filepath(0, outStatusDir, statName.c_str()); return statusFile; }
void TestResult::testOne() { sk_sp<SkPicture> pic; { SkString d; d.printf(" {%d, \"%s\"},", fDirNo, fFilename); SkString path = make_filepath(fDirNo, IN_DIR, fFilename); SkFILEStream stream(path.c_str()); if (!stream.isValid()) { SkDebugf("invalid stream %s\n", path.c_str()); goto finish; } if (fTestStep == kEncodeFiles) { size_t length = stream.getLength(); SkTArray<char, true> bytes; bytes.push_back_n(length); stream.read(&bytes[0], length); stream.rewind(); SkString wPath = make_filepath(0, outSkpDir, fFilename); SkFILEWStream wStream(wPath.c_str()); wStream.write(&bytes[0], length); wStream.flush(); } pic = SkPicture::MakeFromStream(&stream); if (!pic) { SkDebugf("unable to decode %s\n", fFilename); goto finish; } int pWidth = pic->width(); int pHeight = pic->height(); int pLargerWH = SkTMax(pWidth, pHeight); GrContextFactory contextFactory; #ifdef SK_BUILD_FOR_WIN GrContext* context = contextFactory.get(kAngle); #else GrContext* context = contextFactory.get(kNative); #endif if (nullptr == context) { SkDebugf("unable to allocate context for %s\n", fFilename); goto finish; } int maxWH = context->getMaxRenderTargetSize(); int scale = 1; while (pLargerWH / scale > maxWH) { scale *= 2; } SkBitmap bitmap; SkIPoint dim; do { dim.fX = (pWidth + scale - 1) / scale; dim.fY = (pHeight + scale - 1) / scale; bool success = bitmap.allocN32Pixels(dim.fX, dim.fY); if (success) { break; } SkDebugf("-%d-", scale); } while ((scale *= 2) < 256); if (scale >= 256) { SkDebugf("unable to allocate bitmap for %s (w=%d h=%d) (sw=%d sh=%d)\n", fFilename, pWidth, pHeight, dim.fX, dim.fY); return; } SkCanvas skCanvas(bitmap); drawPict(pic, &skCanvas, fScaleOversized ? scale : 1); GrTextureDesc desc; desc.fConfig = kRGBA_8888_GrPixelConfig; desc.fFlags = kRenderTarget_GrTextureFlagBit; desc.fWidth = dim.fX; desc.fHeight = dim.fY; desc.fSampleCnt = 0; sk_sp<GrTexture> texture(context->createUncachedTexture(desc, nullptr, 0)); if (!texture) { SkDebugf("unable to allocate texture for %s (w=%d h=%d)\n", fFilename, dim.fX, dim.fY); return; } SkGpuDevice grDevice(context, texture.get()); SkCanvas grCanvas(&grDevice); drawPict(pic.get(), &grCanvas, fScaleOversized ? scale : 1); SkBitmap grBitmap; grBitmap.allocPixels(grCanvas.imageInfo()); grCanvas.readPixels(&grBitmap, 0, 0); if (fTestStep == kCompareBits) { fPixelError = similarBits(grBitmap, bitmap); SkMSec skTime = timePict(pic, &skCanvas); SkMSec grTime = timePict(pic, &grCanvas); fTime = skTime - grTime; } else if (fTestStep == kEncodeFiles) { SkString pngStr = make_png_name(fFilename); const char* pngName = pngStr.c_str(); writePict(grBitmap, outGrDir, pngName); writePict(bitmap, outSkDir, pngName); } } }