static void test_surface(SkCanvas* canvas, SkSurface* surf) { drawContents(surf, SK_ColorRED); SkImage* imgR = surf->newImageSnapshot(); if (true) { SkImage* imgR2 = surf->newImageSnapshot(); SkASSERT(imgR == imgR2); imgR2->unref(); } drawContents(surf, SK_ColorGREEN); SkImage* imgG = surf->newImageSnapshot(); // since we've drawn after we snapped imgR, imgG will be a different obj SkASSERT(imgR != imgG); drawContents(surf, SK_ColorBLUE); SkPaint paint; // paint.setFilterBitmap(true); // paint.setAlpha(0x80); imgR->draw(canvas, 0, 0, &paint); imgG->draw(canvas, 0, 80, &paint); surf->draw(canvas, 0, 160, &paint); imgG->unref(); imgR->unref(); }
void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) { SkImage* image = this->newImageSnapshot(kYes_Budgeted); if (image) { canvas->drawImage(image, x, y, paint); image->unref(); } }
static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) { drawContents(surf, SK_ColorRED); SkImage* imgR = surf->newImageSnapshot(); if (true) { SkImage* imgR2 = surf->newImageSnapshot(); SkASSERT(imgR == imgR2); imgR2->unref(); } drawContents(surf, SK_ColorGREEN); SkImage* imgG = surf->newImageSnapshot(); // since we've drawn after we snapped imgR, imgG will be a different obj SkASSERT(imgR != imgG); drawContents(surf, SK_ColorBLUE); SkPaint paint; // paint.setFilterBitmap(true); // paint.setAlpha(0x80); canvas->drawImage(imgR, 0, 0, usePaint ? &paint : NULL); canvas->drawImage(imgG, 0, 80, usePaint ? &paint : NULL); surf->draw(canvas, 0, 160, usePaint ? &paint : NULL); SkRect src1, src2, src3; src1.iset(0, 0, surf->width(), surf->height()); src2.iset(-surf->width() / 2, -surf->height() / 2, surf->width(), surf->height()); src3.iset(0, 0, surf->width() / 2, surf->height() / 2); SkRect dst1, dst2, dst3, dst4; dst1.set(0, 240, 65, 305); dst2.set(0, 320, 65, 385); dst3.set(0, 400, 65, 465); dst4.set(0, 480, 65, 545); canvas->drawImageRect(imgR, &src1, dst1, usePaint ? &paint : NULL); canvas->drawImageRect(imgG, &src2, dst2, usePaint ? &paint : NULL); canvas->drawImageRect(imgR, &src3, dst3, usePaint ? &paint : NULL); canvas->drawImageRect(imgG, NULL, dst4, usePaint ? &paint : NULL); imgG->unref(); imgR->unref(); }
static void drawJpeg(SkCanvas* canvas, const SkISize& size) { SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); SkImage* image = SkImage::NewEncodedData(data); if (image) { SkAutoCanvasRestore acr(canvas, true); canvas->scale(size.width() * 1.0f / image->width(), size.height() * 1.0f / image->height()); image->draw(canvas,0, 0, NULL); image->unref(); } }
DEF_TEST(ImageDataRef, reporter) { SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); size_t rowBytes = info.minRowBytes(); size_t size = info.getSafeSize(rowBytes); SkData* data = SkData::NewUninitialized(size); REPORTER_ASSERT(reporter, data->unique()); SkImage* image = SkImage::NewRasterData(info, data, rowBytes); REPORTER_ASSERT(reporter, !data->unique()); image->unref(); REPORTER_ASSERT(reporter, data->unique()); data->unref(); }
static void drawJpeg(SkCanvas* canvas, const SkISize& size) { // TODO: Make this draw a file that is checked in, so it can // be exercised on machines other than mike's. Will require a // rebaseline. SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); SkImage* image = SkImage::NewEncodedData(data); if (image) { SkAutoCanvasRestore acr(canvas, true); canvas->scale(size.width() * 1.0f / image->width(), size.height() * 1.0f / image->height()); image->draw(canvas,0, 0, NULL); image->unref(); } }
static void drawJpeg(SkCanvas* canvas, const SkISize& size) { // TODO: Make this draw a file that is checked in, so it can // be exercised on machines other than mike's. Will require a // rebaseline. SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg")); if (NULL == data.get()) { return; } SkImage* image = SkImage::NewFromGenerator( SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options())); if (image) { SkAutoCanvasRestore acr(canvas, true); canvas->scale(size.width() * 1.0f / image->width(), size.height() * 1.0f / image->height()); canvas->drawImage(image, 0, 0, NULL); image->unref(); } }
virtual void onDraw(SkCanvas* inputCanvas) override { #ifdef SK_BUILD_FOR_ANDROID SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f }; #else SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f }; #endif SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; // set up offscreen rendering with distance field text #if SK_SUPPORT_GPU GrContext* ctx = inputCanvas->getGrContext(); SkImageInfo info = SkImageInfo::MakeN32Premul(onISize()); SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, SkSurfaceProps::kLegacyFontHost_InitType); SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, &props)); SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas; // init our new canvas with the old canvas's matrix canvas->setMatrix(inputCanvas->getTotalMatrix()); #else SkCanvas* canvas = inputCanvas; #endif // apply global scale to test glyph positioning canvas->scale(1.05f, 1.05f); canvas->clear(0xffffffff); SkPaint paint; paint.setAntiAlias(true); paint.setSubpixelText(true); sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypeface::kNormal); const char* text = "Hamburgefons"; const size_t textLen = strlen(text); // check scaling up SkScalar x = SkIntToScalar(0); SkScalar y = SkIntToScalar(78); for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) { SkAutoCanvasRestore acr(canvas, true); canvas->translate(x, y); canvas->scale(scales[i], scales[i]); paint.setTextSize(textSizes[i]); canvas->drawText(text, textLen, 0, 0, paint); y += paint.getFontMetrics(NULL)*scales[i]; } // check rotation for (size_t i = 0; i < 5; ++i) { SkScalar rotX = SkIntToScalar(10); SkScalar rotY = y; SkAutoCanvasRestore acr(canvas, true); canvas->translate(SkIntToScalar(10 + i * 200), -80); rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY); for (int ps = 6; ps <= 32; ps += 3) { paint.setTextSize(SkIntToScalar(ps)); canvas->drawText(text, textLen, rotX, rotY, paint); rotY += paint.getFontMetrics(NULL); } } // check scaling down paint.setLCDRenderText(true); x = SkIntToScalar(680); y = SkIntToScalar(20); size_t arraySize = SK_ARRAY_COUNT(textSizes); for (size_t i = 0; i < arraySize; ++i) { SkAutoCanvasRestore acr(canvas, true); canvas->translate(x, y); SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); canvas->scale(scaleFactor, scaleFactor); paint.setTextSize(textSizes[i]); canvas->drawText(text, textLen, 0, 0, paint); y += paint.getFontMetrics(NULL)*scaleFactor; } // check pos text { SkAutoCanvasRestore acr(canvas, true); canvas->scale(2.0f, 2.0f); SkAutoTArray<SkPoint> pos(SkToInt(textLen)); SkAutoTArray<SkScalar> widths(SkToInt(textLen)); paint.setTextSize(textSizes[0]); paint.getTextWidths(text, textLen, &widths[0]); SkScalar x = SkIntToScalar(340); SkScalar y = SkIntToScalar(75); for (unsigned int i = 0; i < textLen; ++i) { pos[i].set(x, y); x += widths[i]; } canvas->drawPosText(text, textLen, &pos[0], paint); } // check gamma-corrected blending const SkColor fg[] = { 0xFFFFFFFF, 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFF000000, }; paint.setColor(0xFFF1F1F1); SkRect r = SkRect::MakeLTRB(670, 250, 820, 460); canvas->drawRect(r, paint); x = SkIntToScalar(680); y = SkIntToScalar(270); #ifdef SK_BUILD_FOR_ANDROID paint.setTextSize(SkIntToScalar(19)); #else paint.setTextSize(SkIntToScalar(22)); #endif for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { paint.setColor(fg[i]); canvas->drawText(text, textLen, x, y, paint); y += paint.getFontMetrics(NULL); } paint.setColor(0xFF1F1F1F); r = SkRect::MakeLTRB(820, 250, 970, 460); canvas->drawRect(r, paint); x = SkIntToScalar(830); y = SkIntToScalar(270); #ifdef SK_BUILD_FOR_ANDROID paint.setTextSize(SkIntToScalar(19)); #else paint.setTextSize(SkIntToScalar(22)); #endif for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { paint.setColor(fg[i]); canvas->drawText(text, textLen, x, y, paint); y += paint.getFontMetrics(NULL); } // check skew { paint.setLCDRenderText(false); SkAutoCanvasRestore acr(canvas, true); canvas->skew(0.0f, 0.151515f); paint.setTextSize(SkIntToScalar(32)); canvas->drawText(text, textLen, 745, 70, paint); } { paint.setLCDRenderText(true); SkAutoCanvasRestore acr(canvas, true); canvas->skew(0.5f, 0.0f); paint.setTextSize(SkIntToScalar(32)); canvas->drawText(text, textLen, 580, 230, paint); } // check color emoji paint.setTypeface(fTypeface); #ifdef SK_BUILD_FOR_ANDROID paint.setTextSize(SkIntToScalar(19)); #else paint.setTextSize(SkIntToScalar(22)); #endif canvas->drawText(text, textLen, 670, 100, paint); #if SK_SUPPORT_GPU // render offscreen buffer if (surface) { SkAutoCanvasRestore acr(inputCanvas, true); // since we prepended this matrix already, we blit using identity inputCanvas->resetMatrix(); SkImage* image = surface->newImageSnapshot(); inputCanvas->drawImage(image, 0, 0, NULL); image->unref(); } #endif }