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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkShader.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/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class GrContext;
|
2015-08-20 19:30:20 +00:00
|
|
|
|
|
|
|
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;
|
2020-05-21 16:11:27 +00:00
|
|
|
const SkMatrix localM = SkMatrix::Translate(-50, -50);
|
2015-08-20 19:30:20 +00:00
|
|
|
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; )
|