2012-07-29 20:38:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkData.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkEncodedImageFormat.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageEncoder.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/private/SkMalloc.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkAutoPixmapStorage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/core/SkReadBuffer.h"
|
|
|
|
#include "src/core/SkWriteBuffer.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2012-07-31 03:57:11 +00:00
|
|
|
|
2018-09-19 15:31:27 +00:00
|
|
|
#include <functional>
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-07-16 01:34:48 +00:00
|
|
|
const SkSamplingOptions gSamplings[] = {
|
|
|
|
SkSamplingOptions(SkFilterMode::kNearest),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
|
|
|
|
SkSamplingOptions(SkCubicResampler::Mitchell()),
|
|
|
|
};
|
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
static void draw_contents(SkSurface* surface, SkColor fillC) {
|
2012-09-07 02:01:30 +00:00
|
|
|
SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
|
2012-09-06 18:43:21 +00:00
|
|
|
SkIntToScalar(surface->height()));
|
2012-07-31 02:45:15 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2012-07-29 20:38:16 +00:00
|
|
|
|
|
|
|
SkScalar stroke = size.fWidth / 10;
|
|
|
|
SkScalar radius = (size.fWidth - stroke) / 2;
|
|
|
|
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(fillC);
|
|
|
|
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(stroke);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:52:16 +00:00
|
|
|
static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
|
2022-04-20 18:57:03 +00:00
|
|
|
draw_contents(surf, SK_ColorRED);
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> imgR = surf->makeImageSnapshot();
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
if (true) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> imgR2 = surf->makeImageSnapshot();
|
2012-08-28 12:19:02 +00:00
|
|
|
SkASSERT(imgR == imgR2);
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
imgR = ToolUtils::MakeTextureImage(canvas, std::move(imgR));
|
|
|
|
draw_contents(surf, SK_ColorGREEN);
|
|
|
|
sk_sp<SkImage> imgG = ToolUtils::MakeTextureImage(canvas, surf->makeImageSnapshot());
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
// since we've drawn after we snapped imgR, imgG will be a different obj unless the
|
|
|
|
// gpu context has been abandoned (in which case they will both be null)
|
|
|
|
SkASSERT(imgR != imgG || (!imgR && !imgG));
|
2012-08-28 12:19:02 +00:00
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
draw_contents(surf, SK_ColorBLUE);
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
SkSamplingOptions sampling;
|
2012-07-31 02:45:15 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImage(imgR, 0, 0, sampling, usePaint ? &paint : nullptr);
|
|
|
|
canvas->drawImage(imgG, 0, 80, sampling, usePaint ? &paint : nullptr);
|
2021-01-06 13:43:51 +00:00
|
|
|
surf->draw(canvas, 0, 160, SkSamplingOptions(), usePaint ? &paint : nullptr);
|
2013-07-23 15:52:16 +00:00
|
|
|
|
|
|
|
SkRect src1, src2, src3;
|
2019-08-24 23:39:13 +00:00
|
|
|
src1.setIWH(surf->width(), surf->height());
|
|
|
|
src2.setLTRB(SkIntToScalar(-surf->width() / 2), SkIntToScalar(-surf->height() / 2),
|
|
|
|
SkIntToScalar(surf->width()), SkIntToScalar(surf->height()));
|
|
|
|
src3.setIWH(surf->width() / 2, surf->height() / 2);
|
2013-07-23 15:52:16 +00:00
|
|
|
|
|
|
|
SkRect dst1, dst2, dst3, dst4;
|
2019-08-24 23:39:13 +00:00
|
|
|
dst1.setLTRB(0, 240, 65, 305);
|
|
|
|
dst2.setLTRB(0, 320, 65, 385);
|
|
|
|
dst3.setLTRB(0, 400, 65, 465);
|
|
|
|
dst4.setLTRB(0, 480, 65, 545);
|
2013-07-23 15:52:16 +00:00
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImageRect(imgR, src1, dst1, sampling, usePaint ? &paint : nullptr,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
|
|
|
canvas->drawImageRect(imgG, src2, dst2, sampling, usePaint ? &paint : nullptr,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
|
|
|
canvas->drawImageRect(imgR, src3, dst3, sampling, usePaint ? &paint : nullptr,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
|
|
|
canvas->drawImageRect(imgG, dst4, sampling, usePaint ? &paint : nullptr);
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ImageGM : public skiagm::GM {
|
|
|
|
void* fBuffer;
|
2012-07-30 18:20:12 +00:00
|
|
|
size_t fBufferSize;
|
2012-07-29 20:38:16 +00:00
|
|
|
SkSize fSize;
|
|
|
|
enum {
|
|
|
|
W = 64,
|
|
|
|
H = 64,
|
|
|
|
RB = W * 4 + 8,
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
ImageGM() {
|
2012-07-30 18:20:12 +00:00
|
|
|
fBufferSize = RB * H;
|
|
|
|
fBuffer = sk_malloc_throw(fBufferSize);
|
2012-07-29 20:38:16 +00:00
|
|
|
fSize.set(SkIntToScalar(W), SkIntToScalar(H));
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2017-03-22 16:05:03 +00:00
|
|
|
~ImageGM() override {
|
2012-07-29 20:38:16 +00:00
|
|
|
sk_free(fBuffer);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-10-31 14:48:39 +00:00
|
|
|
return SkString("image-surface");
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-07-23 15:52:16 +00:00
|
|
|
return SkISize::Make(960, 1200);
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-07-31 02:45:15 +00:00
|
|
|
canvas->scale(2, 2);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 8);
|
2019-01-07 21:01:22 +00:00
|
|
|
|
|
|
|
canvas->drawString("Original Img", 10, 60, font, SkPaint());
|
|
|
|
canvas->drawString("Modified Img", 10, 140, font, SkPaint());
|
|
|
|
canvas->drawString("Cur Surface", 10, 220, font, SkPaint());
|
|
|
|
canvas->drawString("Full Crop", 10, 300, font, SkPaint());
|
|
|
|
canvas->drawString("Over-crop", 10, 380, font, SkPaint());
|
|
|
|
canvas->drawString("Upper-left", 10, 460, font, SkPaint());
|
|
|
|
canvas->drawString("No Crop", 10, 540, font, SkPaint());
|
|
|
|
|
|
|
|
canvas->drawString("Pre-Alloc Img", 80, 10, font, SkPaint());
|
|
|
|
canvas->drawString("New Alloc Img", 160, 10, font, SkPaint());
|
|
|
|
canvas->drawString( "GPU", 265, 10, font, SkPaint());
|
2012-10-31 14:48:39 +00:00
|
|
|
|
|
|
|
canvas->translate(80, 20);
|
|
|
|
|
2012-07-30 18:20:12 +00:00
|
|
|
// since we draw into this directly, we need to start fresh
|
|
|
|
sk_bzero(fBuffer, fBufferSize);
|
|
|
|
|
2014-01-15 02:38:22 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> surf0(SkSurface::MakeRasterDirect(info, fBuffer, RB));
|
|
|
|
sk_sp<SkSurface> surf1(SkSurface::MakeRaster(info));
|
2020-07-07 14:20:27 +00:00
|
|
|
sk_sp<SkSurface> surf2(SkSurface::MakeRenderTarget(canvas->recordingContext(),
|
2018-05-31 18:27:17 +00:00
|
|
|
SkBudgeted::kNo, info));
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
test_surface(canvas, surf0.get(), true);
|
2013-07-23 15:52:16 +00:00
|
|
|
canvas->translate(80, 0);
|
2016-03-24 01:59:25 +00:00
|
|
|
test_surface(canvas, surf1.get(), true);
|
2015-01-16 22:21:40 +00:00
|
|
|
if (surf2) {
|
2012-10-31 14:48:39 +00:00
|
|
|
canvas->translate(80, 0);
|
2016-03-24 01:59:25 +00:00
|
|
|
test_surface(canvas, surf2.get(), true);
|
2012-10-31 14:48:39 +00:00
|
|
|
}
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2012-07-29 20:38:16 +00:00
|
|
|
};
|
2015-01-16 22:21:40 +00:00
|
|
|
DEF_GM( return new ImageGM; )
|
2015-11-23 20:32:16 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) {
|
|
|
|
SkBitmap bitmap;
|
2015-12-09 19:36:59 +00:00
|
|
|
bitmap.installPixels(pmap);
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImage(bitmap.asImage(), 0, 0);
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImage(image, 0, 0);
|
2015-11-23 20:32:16 +00:00
|
|
|
canvas->translate(110, 10);
|
|
|
|
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40);
|
|
|
|
SkAutoPixmapStorage storage;
|
|
|
|
storage.alloc(info);
|
|
|
|
|
|
|
|
const SkImage::CachingHint chints[] = {
|
2015-11-24 19:44:47 +00:00
|
|
|
SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint,
|
2015-11-23 20:32:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto ch : chints) {
|
|
|
|
canvas->save();
|
2021-07-16 01:34:48 +00:00
|
|
|
for (auto s : gSamplings) {
|
|
|
|
if (image->scalePixels(storage, s, ch)) {
|
2015-11-23 20:32:16 +00:00
|
|
|
draw_pixmap(canvas, storage);
|
|
|
|
}
|
|
|
|
canvas->translate(70, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, 45);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_contents(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(20);
|
|
|
|
canvas->drawCircle(50, 50, 35, paint);
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_raster(const SkImageInfo& info,
|
|
|
|
GrRecordingContext*,
|
|
|
|
void (*draw)(SkCanvas*)) {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-12-02 22:19:47 +00:00
|
|
|
draw(surface->getCanvas());
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_picture(const SkImageInfo& info,
|
|
|
|
GrRecordingContext*,
|
|
|
|
void (*draw)(SkCanvas*)) {
|
2015-11-23 20:32:16 +00:00
|
|
|
SkPictureRecorder recorder;
|
2015-12-02 22:19:47 +00:00
|
|
|
draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
|
2016-03-18 14:25:55 +00:00
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
2017-01-09 17:38:59 +00:00
|
|
|
info.dimensions(), nullptr, nullptr, SkImage::BitDepth::kU8,
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB());
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_codec(const SkImageInfo& info,
|
|
|
|
GrRecordingContext*,
|
|
|
|
void (*draw)(SkCanvas*)) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(make_raster(info, nullptr, draw));
|
2017-07-11 20:03:13 +00:00
|
|
|
return SkImage::MakeFromEncoded(image->encodeToData());
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_gpu(const SkImageInfo& info,
|
|
|
|
GrRecordingContext* ctx,
|
|
|
|
void (*draw)(SkCanvas*)) {
|
|
|
|
if (!ctx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
|
2020-07-07 14:20:27 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-02 22:19:47 +00:00
|
|
|
draw(surface->getCanvas());
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&,
|
|
|
|
GrRecordingContext*,
|
|
|
|
void (*)(SkCanvas*));
|
2015-11-23 20:32:16 +00:00
|
|
|
|
|
|
|
class ScalePixelsGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ScalePixelsGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("scale-pixels");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(960, 1200);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
|
|
|
|
const ImageMakerProc procs[] = {
|
2015-11-24 19:44:47 +00:00
|
|
|
make_codec, make_raster, make_picture, make_codec, make_gpu,
|
2015-11-23 20:32:16 +00:00
|
|
|
};
|
|
|
|
for (auto& proc : procs) {
|
2020-07-07 14:20:27 +00:00
|
|
|
sk_sp<SkImage> image(proc(info, canvas->recordingContext(), draw_contents));
|
2015-11-23 20:32:16 +00:00
|
|
|
if (image) {
|
2016-03-17 17:51:11 +00:00
|
|
|
show_scaled_pixels(canvas, image.get());
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
canvas->translate(0, 120);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-11-23 20:32:16 +00:00
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2015-11-23 20:32:16 +00:00
|
|
|
};
|
|
|
|
DEF_GM( return new ScalePixelsGM; )
|
2015-12-02 22:19:47 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
DEF_SIMPLE_GPU_GM(new_texture_image, rContext, canvas, 280, 60) {
|
|
|
|
auto dContext = rContext->asDirectContext();
|
|
|
|
if (!dContext) {
|
2020-06-29 19:36:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-07 16:23:28 +00:00
|
|
|
auto render_image = [](SkCanvas* canvas) {
|
|
|
|
canvas->clear(SK_ColorBLUE);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr int kSize = 50;
|
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocPixels(SkImageInfo::MakeS32(kSize, kSize, kPremul_SkAlphaType));
|
|
|
|
SkCanvas bmpCanvas(bmp);
|
|
|
|
render_image(&bmpCanvas);
|
|
|
|
|
|
|
|
std::function<sk_sp<SkImage>()> imageFactories[] = {
|
|
|
|
// Create sw raster image.
|
|
|
|
[bmp] {
|
2020-12-23 15:11:33 +00:00
|
|
|
return bmp.asImage();
|
2017-02-07 16:23:28 +00:00
|
|
|
},
|
|
|
|
// Create encoded image.
|
|
|
|
[bmp] {
|
2018-09-24 19:24:31 +00:00
|
|
|
auto src = SkEncodeBitmap(bmp, SkEncodedImageFormat::kPNG, 100);
|
2017-02-07 16:23:28 +00:00
|
|
|
return SkImage::MakeFromEncoded(std::move(src));
|
|
|
|
},
|
2017-02-07 17:31:02 +00:00
|
|
|
// Create YUV encoded image.
|
|
|
|
[bmp] {
|
2018-09-24 19:24:31 +00:00
|
|
|
auto src = SkEncodeBitmap(bmp, SkEncodedImageFormat::kJPEG, 100);
|
2017-02-07 17:31:02 +00:00
|
|
|
return SkImage::MakeFromEncoded(std::move(src));
|
|
|
|
},
|
2017-02-07 16:23:28 +00:00
|
|
|
// Create a picture image.
|
|
|
|
[render_image] {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize));
|
|
|
|
render_image(canvas);
|
2017-02-07 18:56:11 +00:00
|
|
|
sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
|
2017-02-07 16:23:28 +00:00
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
|
|
|
SkISize::Make(kSize, kSize), nullptr, nullptr,
|
|
|
|
SkImage::BitDepth::kU8, srgbColorSpace);
|
|
|
|
},
|
|
|
|
// Create a texture image
|
2021-07-21 19:39:51 +00:00
|
|
|
[rContext, render_image]() -> sk_sp<SkImage> {
|
|
|
|
auto surface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes,
|
2017-02-07 16:23:28 +00:00
|
|
|
SkImageInfo::MakeS32(kSize, kSize,
|
|
|
|
kPremul_SkAlphaType)));
|
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
render_image(surface->getCanvas());
|
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr SkScalar kPad = 5.f;
|
|
|
|
canvas->translate(kPad, kPad);
|
2020-07-31 00:24:57 +00:00
|
|
|
for (const auto& factory : imageFactories) {
|
|
|
|
sk_sp<SkImage> image(factory());
|
2017-02-07 17:31:02 +00:00
|
|
|
if (image) {
|
2021-07-21 19:39:51 +00:00
|
|
|
sk_sp<SkImage> texImage(image->makeTextureImage(dContext));
|
2017-02-07 16:23:28 +00:00
|
|
|
if (texImage) {
|
|
|
|
canvas->drawImage(texImage, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 13:04:06 +00:00
|
|
|
canvas->translate(kSize + kPad, 0);
|
2017-02-07 16:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-16 15:01:16 +00:00
|
|
|
|
|
|
|
static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pm, SkScalar x, SkScalar y) {
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImage(SkImage::MakeRasterCopy(pm), x, y);
|
2017-11-16 15:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void slam_ff(const SkPixmap& pm) {
|
|
|
|
for (int y = 0; y < pm.height(); ++y) {
|
|
|
|
for (int x = 0; x < pm.width(); ++x) {
|
|
|
|
*pm.writable_addr32(x, y) = *pm.addr32(x, y) | SkPackARGB32(0xFF, 0, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(scalepixels_unpremul, canvas, 1080, 280) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(16, 16, kUnpremul_SkAlphaType);
|
|
|
|
SkAutoPixmapStorage pm;
|
|
|
|
pm.alloc(info);
|
|
|
|
for (int y = 0; y < 16; ++y) {
|
|
|
|
for (int x = 0; x < 16; ++x) {
|
|
|
|
*pm.writable_addr32(x, y) = SkPackARGB32NoCheck(0, (y << 4) | y, (x << 4) | x, 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkAutoPixmapStorage pm2;
|
|
|
|
pm2.alloc(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
|
|
|
|
|
2021-07-16 01:34:48 +00:00
|
|
|
for (auto s : gSamplings) {
|
|
|
|
pm.scalePixels(pm2, s);
|
2017-11-16 15:01:16 +00:00
|
|
|
slam_ff(pm2);
|
|
|
|
draw_pixmap(canvas, pm2, 10, 10);
|
|
|
|
canvas->translate(pm2.width() + 10.0f, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-05-29 19:41:27 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static sk_sp<SkImage> make_lazy_image(SkSurface* surf) {
|
|
|
|
surf->getCanvas()->drawCircle(100, 100, 100, SkPaint());
|
2018-07-13 14:18:38 +00:00
|
|
|
sk_sp<SkData> data = surf->makeImageSnapshot()->encodeToData();
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return SkImage::MakeFromEncoded(std::move(data));
|
2018-05-29 19:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkImage> serial_deserial(SkImage* img) {
|
|
|
|
SkBinaryWriteBuffer writer;
|
|
|
|
writer.writeImage(img);
|
|
|
|
size_t length = writer.bytesWritten();
|
|
|
|
auto data = SkData::MakeUninitialized(length);
|
|
|
|
writer.writeToMemory(data->writable_data());
|
|
|
|
|
|
|
|
SkReadBuffer reader(data->data(), length);
|
|
|
|
return reader.readImage();
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DEF_SIMPLE_GM_CAN_FAIL(image_subset, canvas, errorMsg, 440, 220) {
|
2020-10-14 15:46:51 +00:00
|
|
|
auto rContext = canvas->recordingContext();
|
|
|
|
auto dContext = GrAsDirectContext(rContext);
|
|
|
|
|
|
|
|
if (!dContext && rContext) {
|
|
|
|
*errorMsg = "Not supported in DDL mode";
|
|
|
|
return skiagm::DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2018-05-29 19:41:27 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(200, 200, nullptr);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surf = ToolUtils::makeSurface(canvas, info, nullptr);
|
2018-05-29 19:41:27 +00:00
|
|
|
auto img = make_lazy_image(surf.get());
|
2018-07-13 14:18:38 +00:00
|
|
|
if (!img) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Failed to make lazy image.";
|
|
|
|
return skiagm::DrawResult::kFail;
|
2018-07-13 14:18:38 +00:00
|
|
|
}
|
2018-05-29 19:41:27 +00:00
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImage(img, 10, 10);
|
2018-05-29 19:41:27 +00:00
|
|
|
auto sub = img->makeSubset({100, 100, 200, 200});
|
|
|
|
canvas->drawImage(sub, 220, 10);
|
|
|
|
sub = serial_deserial(sub.get());
|
|
|
|
canvas->drawImage(sub, 220+110, 10);
|
2019-02-07 23:20:09 +00:00
|
|
|
return skiagm::DrawResult::kOk;
|
2018-05-29 19:41:27 +00:00
|
|
|
}
|