| 1 |
/* |
| 2 |
* Copyright 2015 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/codec/SkEncodedImageFormat.h" |
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImage.h" |
| 16 |
#include "include/core/SkImageInfo.h" |
| 17 |
#include "include/core/SkMatrix.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkPicture.h" |
| 20 |
#include "include/core/SkPictureRecorder.h" |
| 21 |
#include "include/core/SkRect.h" |
| 22 |
#include "include/core/SkRefCnt.h" |
| 23 |
#include "include/core/SkSamplingOptions.h" |
| 24 |
#include "include/core/SkShader.h" |
| 25 |
#include "include/core/SkSize.h" |
| 26 |
#include "include/core/SkString.h" |
| 27 |
#include "include/core/SkSurface.h" |
| 28 |
#include "include/core/SkTileMode.h" |
| 29 |
#include "include/core/SkTypes.h" |
| 30 |
#include "include/encode/SkPngEncoder.h" |
| 31 |
#include "include/gpu/GpuTypes.h" |
| 32 |
#include "tools/DecodeUtils.h" |
| 33 |
#include "tools/ToolUtils.h" |
| 34 |
|
| 35 |
#if defined(SK_GANESH) |
| 36 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <utility> |
| 40 |
|
| 41 |
static void draw_something(SkCanvas* canvas, const SkRect& bounds) { |
| 42 |
SkPaint paint; |
| 43 |
paint.setAntiAlias(true); |
| 44 |
paint.setColor(SK_ColorRED); |
| 45 |
paint.setStyle(SkPaint::kStroke_Style); |
| 46 |
paint.setStrokeWidth(10); |
| 47 |
canvas->drawRect(bounds, paint); |
| 48 |
paint.setStyle(SkPaint::kFill_Style); |
| 49 |
paint.setColor(SK_ColorBLUE); |
| 50 |
canvas->drawOval(bounds, paint); |
| 51 |
} |
| 52 |
|
| 53 |
typedef sk_sp<SkImage> (*ImageMakerProc)(GrRecordingContext*, SkPicture*, const SkImageInfo&); |
| 54 |
|
| 55 |
static sk_sp<SkImage> make_raster(GrRecordingContext*, |
| 56 |
SkPicture* pic, |
| 57 |
const SkImageInfo& info) { |
| 58 |
auto surface(SkSurfaces::Raster(info)); |
| 59 |
surface->getCanvas()->clear(0); |
| 60 |
surface->getCanvas()->drawPicture(pic); |
| 61 |
return surface->makeImageSnapshot(); |
| 62 |
} |
| 63 |
|
| 64 |
static sk_sp<SkImage> make_texture(GrRecordingContext* ctx, |
| 65 |
SkPicture* pic, |
| 66 |
const SkImageInfo& info) { |
| 67 |
if (!ctx) { |
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
sk_sp<SkSurface> surface; |
| 71 |
#if defined(SK_GANESH) |
| 72 |
surface = SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info); |
| 73 |
#endif |
| 74 |
if (!surface) { |
| 75 |
return nullptr; |
| 76 |
} |
| 77 |
surface->getCanvas()->clear(0); |
| 78 |
surface->getCanvas()->drawPicture(pic); |
| 79 |
return surface->makeImageSnapshot(); |
| 80 |
} |
| 81 |
|
| 82 |
static sk_sp<SkImage> make_pict_gen(GrRecordingContext*, |
| 83 |
SkPicture* pic, |
| 84 |
const SkImageInfo& info) { |
| 85 |
return SkImages::DeferredFromPicture(sk_ref_sp(pic), |
| 86 |
info.dimensions(), |
| 87 |
nullptr, |
| 88 |
nullptr, |
| 89 |
SkImages::BitDepth::kU8, |
| 90 |
SkColorSpace::MakeSRGB()); |
| 91 |
} |
| 92 |
|
| 93 |
static sk_sp<SkImage> make_encode_gen(GrRecordingContext* ctx, |
| 94 |
SkPicture* pic, |
| 95 |
const SkImageInfo& info) { |
| 96 |
sk_sp<SkImage> src(make_raster(ctx, pic, info)); |
| 97 |
if (!src) { |
| 98 |
return nullptr; |
| 99 |
} |
| 100 |
sk_sp<SkData> encoded = SkPngEncoder::Encode(nullptr, src.get(), {}); |
| 101 |
if (!encoded) { |
| 102 |
return nullptr; |
| 103 |
} |
| 104 |
return SkImages::DeferredFromEncodedData(std::move(encoded)); |
| 105 |
} |
| 106 |
|
| 107 |
const ImageMakerProc gProcs[] = { |
| 108 |
make_raster, |
| 109 |
make_texture, |
| 110 |
make_pict_gen, |
| 111 |
make_encode_gen, |
| 112 |
}; |
| 113 |
|
| 114 |
/* |
| 115 |
* Exercise drawing pictures inside an image, showing that the image version is pixelated |
| 116 |
* (correctly) when it is inside an image. |
| 117 |
*/ |
| 118 |
class ImageShaderGM : public skiagm::GM { |
| 119 |
sk_sp<SkPicture> fPicture; |
| 120 |
|
| 121 |
public: |
| 122 |
ImageShaderGM() {} |
| 123 |
|
| 124 |
protected: |
| 125 |
SkString getName() const override { return SkString("image-shader"); } |
| 126 |
|
| 127 |
SkISize getISize() override { return SkISize::Make(850, 450); } |
| 128 |
|
| 129 |
void onOnceBeforeDraw() override { |
| 130 |
const SkRect bounds = SkRect::MakeWH(100, 100); |
| 131 |
SkPictureRecorder recorder; |
| 132 |
draw_something(recorder.beginRecording(bounds), bounds); |
| 133 |
fPicture = recorder.finishRecordingAsPicture(); |
| 134 |
} |
| 135 |
|
| 136 |
void testImage(SkCanvas* canvas, SkImage* image) { |
| 137 |
SkAutoCanvasRestore acr(canvas, true); |
| 138 |
|
| 139 |
canvas->drawImage(image, 0, 0); |
| 140 |
canvas->translate(0, 120); |
| 141 |
|
| 142 |
const SkTileMode tile = SkTileMode::kRepeat; |
| 143 |
const SkMatrix localM = SkMatrix::Translate(-50, -50); |
| 144 |
SkPaint paint; |
| 145 |
paint.setShader(image->makeShader(tile, tile, SkSamplingOptions(), &localM)); |
| 146 |
paint.setAntiAlias(true); |
| 147 |
canvas->drawCircle(50, 50, 50, paint); |
| 148 |
} |
| 149 |
|
| 150 |
void onDraw(SkCanvas* canvas) override { |
| 151 |
canvas->translate(20, 20); |
| 152 |
|
| 153 |
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 154 |
|
| 155 |
for (size_t i = 0; i < std::size(gProcs); ++i) { |
| 156 |
sk_sp<SkImage> image(gProcs[i](canvas->recordingContext(), fPicture.get(), info)); |
| 157 |
if (image) { |
| 158 |
this->testImage(canvas, image.get()); |
| 159 |
} |
| 160 |
canvas->translate(120, 0); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
private: |
| 165 |
using INHERITED = skiagm::GM; |
| 166 |
}; |
| 167 |
DEF_GM( return new ImageShaderGM; ) |
| 168 |
|
| 169 |
////////////////////////////////////////////////////////////////////////////////////////////// |
| 170 |
|
| 171 |
#include "tools/ToolUtils.h" |
| 172 |
|
| 173 |
static sk_sp<SkImage> make_checker_img(int w, int h, SkColor c0, SkColor c1, int size) { |
| 174 |
SkBitmap bm = ToolUtils::create_checkerboard_bitmap(w, h, c0, c1, size); |
| 175 |
bm.setImmutable(); |
| 176 |
return bm.asImage(); |
| 177 |
} |
| 178 |
|
| 179 |
DEF_SIMPLE_GM(drawimage_sampling, canvas, 500, 500) { |
| 180 |
constexpr int N = 256; |
| 181 |
constexpr float kScale = 1.0f/6; |
| 182 |
const SkRect dst = {0, 0, kScale*N, kScale*N}; |
| 183 |
|
| 184 |
auto img = make_checker_img(N, N, SK_ColorBLACK, SK_ColorWHITE, 7)->withDefaultMipmaps(); |
| 185 |
const SkRect src = SkRect::MakeIWH(img->width(), img->height()); |
| 186 |
|
| 187 |
SkMatrix mx = SkMatrix::RectToRectOrIdentity(src, dst); |
| 188 |
|
| 189 |
SkPaint paint; |
| 190 |
|
| 191 |
for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) { |
| 192 |
for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) { |
| 193 |
SkSamplingOptions sampling(fm, mm); |
| 194 |
|
| 195 |
canvas->save(); |
| 196 |
|
| 197 |
canvas->save(); |
| 198 |
canvas->concat(mx); |
| 199 |
canvas->drawImage(img.get(), 0, 0, sampling); |
| 200 |
canvas->restore(); |
| 201 |
|
| 202 |
canvas->translate(dst.width() + 4, 0); |
| 203 |
|
| 204 |
paint.setShader(img->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, &mx)); |
| 205 |
canvas->drawRect(dst, paint); |
| 206 |
|
| 207 |
canvas->translate(dst.width() + 4, 0); |
| 208 |
|
| 209 |
canvas->drawImageRect(img.get(), src, dst, sampling, nullptr, |
| 210 |
SkCanvas::kFast_SrcRectConstraint); |
| 211 |
canvas->restore(); |
| 212 |
|
| 213 |
canvas->translate(0, dst.height() + 8); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
// Test case for skbug.com/40043768 (texture-backed image shaders silently fail drawing to CPU canvas) |
| 220 |
DEF_SIMPLE_GM(textureimage_and_shader, canvas, 100, 50) { |
| 221 |
canvas->clear(SK_ColorGREEN); |
| 222 |
|
| 223 |
sk_sp<SkImage> image; |
| 224 |
if (canvas->getSurface()) { |
| 225 |
image = canvas->getSurface()->makeImageSnapshot(); |
| 226 |
canvas->clear(SK_ColorRED); |
| 227 |
} else { |
| 228 |
auto greenSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)); |
| 229 |
greenSurface->getCanvas()->clear(SK_ColorGREEN); |
| 230 |
image = greenSurface->makeImageSnapshot(); |
| 231 |
} |
| 232 |
|
| 233 |
// At this point, 'image' contains a green image. If our original canvas is GPU-backed, then |
| 234 |
// the snapped image will be a (GPU) texture. We will try to draw that image to a non-GPU |
| 235 |
// surface, to ensure that we get automatic read-back. If all goes well, we will get a pure |
| 236 |
// green result. If either draw fails, we'll get red (most likely). |
| 237 |
|
| 238 |
auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50)); |
| 239 |
|
| 240 |
// First, use drawImage: |
| 241 |
surface->getCanvas()->clear(SK_ColorRED); |
| 242 |
surface->getCanvas()->drawImage(image, 0, 0); |
| 243 |
canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 244 |
|
| 245 |
// Now, use an image shader: |
| 246 |
SkPaint paint; |
| 247 |
paint.setShader(image->makeShader(SkSamplingOptions())); |
| 248 |
surface->getCanvas()->clear(SK_ColorRED); |
| 249 |
surface->getCanvas()->drawPaint(paint); |
| 250 |
canvas->drawImage(surface->makeImageSnapshot(), 50, 0); |
| 251 |
} |
| 252 |
|
| 253 |
DEF_SIMPLE_GM(imageshader_tinyscale, canvas, 1000, 1000) { |
| 254 |
// 128x128px image with red/black/green/blue quadrants. |
| 255 |
auto img = ToolUtils::GetResourceAsImage("images/gainmap_gcontainer_only.jpg"); |
| 256 |
|
| 257 |
// A small scale amplifies the sampling coords in image space. |
| 258 |
static constexpr float kScale = 0.01f; |
| 259 |
const auto m = SkMatrix::Translate(500, 500) * |
| 260 |
SkMatrix::Scale(kScale, kScale); |
| 261 |
|
| 262 |
// In clamp mode we should see no repeating patterns, just the viewport filled |
| 263 |
// with four-colored quadrants. |
| 264 |
SkPaint p; |
| 265 |
p.setShader(img->makeShader(SkTileMode::kClamp, |
| 266 |
SkTileMode::kClamp, |
| 267 |
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone), |
| 268 |
&m)); |
| 269 |
canvas->drawPaint(p); |
| 270 |
} |