2015-08-20 19:30:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
|
|
|
static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(10);
|
|
|
|
canvas->drawRect(bounds, paint);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawOval(bounds, paint);
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
typedef sk_sp<SkImage> (*ImageMakerProc)(GrContext*, SkPicture*, const SkImageInfo&);
|
2015-08-20 19:30:20 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_raster(GrContext*, SkPicture* pic, const SkImageInfo& info) {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-08-20 19:30:20 +00:00
|
|
|
surface->getCanvas()->clear(0);
|
|
|
|
surface->getCanvas()->drawPicture(pic);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-08-20 19:30:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
|
2015-08-20 19:30:20 +00:00
|
|
|
if (!ctx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
|
2016-05-12 13:19:37 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-20 19:30:20 +00:00
|
|
|
surface->getCanvas()->clear(0);
|
|
|
|
surface->getCanvas()->drawPicture(pic);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-08-20 19:30:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
|
2016-12-16 16:55:18 +00:00
|
|
|
return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
|
2017-01-09 17:38:59 +00:00
|
|
|
SkImage::BitDepth::kU8,
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB());
|
2015-08-20 19:30:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
|
|
|
|
sk_sp<SkImage> src(make_raster(ctx, pic, info));
|
2015-08-20 19:30:20 +00:00
|
|
|
if (!src) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-11 20:03:13 +00:00
|
|
|
sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100);
|
2015-08-20 19:30:20 +00:00
|
|
|
if (!encoded) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromEncoded(std::move(encoded));
|
2015-08-20 19:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ImageMakerProc gProcs[] = {
|
|
|
|
make_raster,
|
|
|
|
make_texture,
|
|
|
|
make_pict_gen,
|
|
|
|
make_encode_gen,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exercise drawing pictures inside an image, showing that the image version is pixelated
|
|
|
|
* (correctly) when it is inside an image.
|
|
|
|
*/
|
|
|
|
class ImageShaderGM : public skiagm::GM {
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> fPicture;
|
2015-08-20 19:30:20 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ImageShaderGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("image-shader");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(850, 450);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
const SkRect bounds = SkRect::MakeWH(100, 100);
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
draw_something(recorder.beginRecording(bounds), bounds);
|
2016-03-18 14:25:55 +00:00
|
|
|
fPicture = recorder.finishRecordingAsPicture();
|
2015-08-20 19:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void testImage(SkCanvas* canvas, SkImage* image) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->drawImage(image, 0, 0);
|
|
|
|
canvas->translate(0, 120);
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
const SkTileMode tile = SkTileMode::kRepeat;
|
2015-08-20 19:30:20 +00:00
|
|
|
const SkMatrix localM = SkMatrix::MakeTrans(-50, -50);
|
|
|
|
SkPaint paint;
|
2016-03-09 22:47:34 +00:00
|
|
|
paint.setShader(image->makeShader(tile, tile, &localM));
|
2015-08-20 19:30:20 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawCircle(50, 50, 50, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->translate(20, 20);
|
|
|
|
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info));
|
2015-08-20 19:30:20 +00:00
|
|
|
if (image) {
|
2016-03-17 17:51:11 +00:00
|
|
|
this->testImage(canvas, image.get());
|
2015-08-20 19:30:20 +00:00
|
|
|
}
|
|
|
|
canvas->translate(120, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ImageShaderGM; )
|