void onDraw(SkCanvas* canvas) override { canvas->clear(SK_ColorBLACK); SkPaint paint; for (int i = 0; i < 4; i++) { SkImage* image = (i & 0x01) ? fCheckerboard.get() : fBitmap.get(); SkIRect cropRect = SkIRect::MakeXYWH(i * 12, i * 8, image->width() - i * 8, image->height() - i * 12); SkImageFilter::CropRect rect(SkRect::Make(cropRect)); SkAutoTUnref<SkImageFilter> tileInput(SkImageSource::Create(image)); SkScalar dx = SkIntToScalar(i*5); SkScalar dy = SkIntToScalar(i*10); SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, dy, tileInput, &rect)); paint.setImageFilter(filter); DrawClippedImage(canvas, image, paint, 1, cropRect); canvas->translate(SkIntToScalar(image->width() + MARGIN), 0); } SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); SkImageFilter::CropRect rect(SkRect::Make(cropRect)); SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10, nullptr, &rect)); paint.setImageFilter(filter); DrawClippedImage(canvas, fBitmap.get(), paint, 2, cropRect); }
static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* target, bool sendBinaries) { if (sendBinaries) { SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100); if (encoded == nullptr) { // PNG encode doesn't necessarily support all color formats, convert to a different // format size_t rowBytes = 4 * image.width(); void* buffer = sk_malloc_throw(rowBytes * image.height()); SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), kN32_SkColorType, kPremul_SkAlphaType); if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) { SkDebugf("readPixels failed\n"); return false; } SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes); encoded = converted->encode(SkImageEncoder::kPNG_Type, 100); if (encoded == nullptr) { SkDebugf("image encode failed\n"); return false; } free(converted); free(buffer); } Json::Value bytes; encode_data(encoded->data(), encoded->size(), &bytes); (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes; encoded->unref(); } else { SkString description = SkStringPrintf("%dx%d pixel image", image.width(), image.height()); (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description.c_str()); } return true; }
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 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 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) { // 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(); } }
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 HelloWorldWindow::draw(SkCanvas* canvas) { drawContents(canvas); // in case we have queued drawing calls fContext->flush(); // Invalidate the window to force a redraw. Poor man's animation mechanism. this->inval(NULL); if (kRaster_DeviceType == fType) { // need to send the raster bits to the (gpu) window SkImage* snap = fSurface->newImageSnapshot(); size_t rowBytes; SkImageInfo info; const void* pixels = snap->peekPixels(&info, &rowBytes); fRenderTarget->writePixels(0, 0, snap->width(), snap->height(), SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), info.profileType()), pixels, rowBytes, GrContext::kFlushWrites_PixelOp); SkSafeUnref(snap); } INHERITED::present(); }
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 }