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"
|
2020-12-23 15:11:33 +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"
|
|
|
|
#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>
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
typedef sk_sp<SkImage> (*ImageMakerProc)(GrRecordingContext*, SkPicture*, const SkImageInfo&);
|
2015-08-20 19:30:20 +00:00
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_raster(GrRecordingContext*,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_texture(GrRecordingContext* 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
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_pict_gen(GrRecordingContext*,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 14:20:27 +00:00
|
|
|
static sk_sp<SkImage> make_encode_gen(GrRecordingContext* ctx,
|
|
|
|
SkPicture* pic,
|
|
|
|
const SkImageInfo& info) {
|
2016-03-17 17:51:11 +00:00
|
|
|
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;
|
2020-12-09 02:58:35 +00:00
|
|
|
paint.setShader(image->makeShader(tile, tile, SkSamplingOptions(), &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) {
|
2020-07-07 14:20:27 +00:00
|
|
|
sk_sp<SkImage> image(gProcs[i](canvas->recordingContext(), 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:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2015-08-20 19:30:20 +00:00
|
|
|
};
|
|
|
|
DEF_GM( return new ImageShaderGM; )
|
2020-12-07 17:29:18 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
static sk_sp<SkImage> make_checker_img(int w, int h, SkColor c0, SkColor c1, int size) {
|
|
|
|
SkBitmap bm = ToolUtils::create_checkerboard_bitmap(w, h, c0, c1, size);
|
|
|
|
bm.setImmutable();
|
2020-12-23 15:11:33 +00:00
|
|
|
return bm.asImage();
|
2020-12-07 17:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(drawimage_sampling, canvas, 500, 500) {
|
|
|
|
constexpr int N = 256;
|
|
|
|
constexpr float kScale = 1.0f/6;
|
|
|
|
const SkRect dst = {0, 0, kScale*N, kScale*N};
|
|
|
|
|
|
|
|
auto img = make_checker_img(N, N, SK_ColorBLACK, SK_ColorWHITE, 7)->withDefaultMipmaps();
|
|
|
|
const SkRect src = SkRect::MakeIWH(img->width(), img->height());
|
|
|
|
|
2021-01-15 17:26:22 +00:00
|
|
|
SkMatrix mx = SkMatrix::RectToRect(src, dst);
|
2020-12-07 17:29:18 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
|
|
|
|
for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
|
|
|
|
SkSamplingOptions sampling(fm, mm);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(mx);
|
|
|
|
canvas->drawImage(img.get(), 0, 0, sampling);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dst.width() + 4, 0);
|
|
|
|
|
|
|
|
paint.setShader(img->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, &mx));
|
|
|
|
canvas->drawRect(dst, paint);
|
|
|
|
|
|
|
|
canvas->translate(dst.width() + 4, 0);
|
|
|
|
|
Reland "Add new virts, hide old ones"
This reverts commit 8f924ac0ce63806886b7297e8be554984a6e7ce5.
Reason for revert: suppressions landed for fuchsia images to rebaseline
Original change's description:
> Revert "Add new virts, hide old ones"
>
> This reverts commit c56e2e5aa65dd129e5927224d2f6c1f82edff74e.
>
> Reason for revert: suspected of breaking chrome roll
>
> Original change's description:
> > Add new virts, hide old ones
> >
> > Add virtuals for the draw methods that now take sampling/filtermode.
> >
> > drawImage
> > drawImageRect
> > drawImageLattice
> > drawAtlas
> >
> > Add a flag that can remove the older virtuals, once each client has
> > stopped overriding them. In that situation, the older public methods
> > will simplify extract the sampling from the paint, and call the new
> > public methods.
> >
> > Bug: skia:11105, skia:7650
> > Change-Id: I8b0029727295caa983e8148fc743a55cfbecd043
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/347022
> > Commit-Queue: Mike Reed <reed@google.com>
> > Reviewed-by: Florin Malita <fmalita@chromium.org>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
>
> TBR=bsalomon@google.com,fmalita@chromium.org,reed@google.com
>
> Change-Id: I0a90952c11a180d918126ea06a630f4a0bf9b49b
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:11105
> Bug: skia:7650
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348194
> Reviewed-by: Derek Sollenberger <djsollen@google.com>
> Commit-Queue: Derek Sollenberger <djsollen@google.com>
TBR=djsollen@google.com,bsalomon@google.com,fmalita@chromium.org,reed@google.com
# Not skipping CQ checks because this is a reland.
Bug: skia:11105
Bug: skia:7650
Change-Id: Ia2b4537a2d330460b7554278d2c05075cf27162a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348876
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2020-12-30 14:22:42 +00:00
|
|
|
canvas->drawImageRect(img.get(), src, dst, sampling, nullptr,
|
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
2020-12-07 17:29:18 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(0, dst.height() + 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-12-01 20:35:38 +00:00
|
|
|
|
|
|
|
// Test case for skbug.com/12685 (texture-backed image shaders silently fail drawing to CPU canvas)
|
|
|
|
DEF_SIMPLE_GM(textureimage_and_shader, canvas, 100, 50) {
|
|
|
|
canvas->clear(SK_ColorGREEN);
|
|
|
|
|
|
|
|
sk_sp<SkImage> image;
|
|
|
|
if (canvas->getSurface()) {
|
|
|
|
image = canvas->getSurface()->makeImageSnapshot();
|
|
|
|
canvas->clear(SK_ColorRED);
|
|
|
|
} else {
|
|
|
|
auto greenSurface = SkSurface::MakeRasterN32Premul(50, 50);
|
|
|
|
greenSurface->getCanvas()->clear(SK_ColorGREEN);
|
|
|
|
image = greenSurface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, 'image' contains a green image. If our original canvas is GPU-backed, then
|
|
|
|
// the snapped image will be a (GPU) texture. We will try to draw that image to a non-GPU
|
|
|
|
// surface, to ensure that we get automatic read-back. If all goes well, we will get a pure
|
|
|
|
// green result. If either draw fails, we'll get red (most likely).
|
|
|
|
|
|
|
|
auto surface = SkSurface::MakeRasterN32Premul(50, 50);
|
|
|
|
|
|
|
|
// First, use drawImage:
|
|
|
|
surface->getCanvas()->clear(SK_ColorRED);
|
|
|
|
surface->getCanvas()->drawImage(image, 0, 0);
|
|
|
|
canvas->drawImage(surface->makeImageSnapshot(), 0, 0);
|
|
|
|
|
|
|
|
// Now, use an image shader:
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(image->makeShader(SkSamplingOptions()));
|
|
|
|
surface->getCanvas()->clear(SK_ColorRED);
|
|
|
|
surface->getCanvas()->drawPaint(paint);
|
|
|
|
canvas->drawImage(surface->makeImageSnapshot(), 50, 0);
|
|
|
|
}
|