blob: da4a79b359a2f0da36911fb6a300b7414518ddca [file] [log] [blame]
reedd5b75632015-08-13 09:37:45 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkColorSpace.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImage.h"
14#include "include/core/SkImageGenerator.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkImageInfo.h"
16#include "include/core/SkMatrix.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPicture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkPoint.h"
21#include "include/core/SkRect.h"
22#include "include/core/SkRefCnt.h"
23#include "include/core/SkScalar.h"
24#include "include/core/SkSize.h"
25#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "include/gpu/GrTypes.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040032#include "src/gpu/GrSamplerState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040034#include "src/gpu/GrTextureProxy.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040035#include "src/image/SkImage_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/image/SkImage_Gpu.h"
reedd5b75632015-08-13 09:37:45 -070037
Ben Wagner7fde8e12019-05-01 17:28:53 -040038#include <memory>
39#include <utility>
40
41class GrRecordingContext;
42
reedd5b75632015-08-13 09:37:45 -070043static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
44 SkPaint paint;
45 paint.setAntiAlias(true);
46 paint.setColor(SK_ColorRED);
47 paint.setStyle(SkPaint::kStroke_Style);
48 paint.setStrokeWidth(10);
49 canvas->drawRect(bounds, paint);
50 paint.setStyle(SkPaint::kFill_Style);
51 paint.setColor(SK_ColorBLUE);
52 canvas->drawOval(bounds, paint);
53}
54
55/*
56 * Exercise drawing pictures inside an image, showing that the image version is pixelated
57 * (correctly) when it is inside an image.
58 */
59class ImagePictGM : public skiagm::GM {
reedca2622b2016-03-18 07:25:55 -070060 sk_sp<SkPicture> fPicture;
reed9ce9d672016-03-17 10:51:11 -070061 sk_sp<SkImage> fImage0;
62 sk_sp<SkImage> fImage1;
reedd5b75632015-08-13 09:37:45 -070063public:
64 ImagePictGM() {}
65
66protected:
67 SkString onShortName() override {
68 return SkString("image-picture");
69 }
70
71 SkISize onISize() override {
72 return SkISize::Make(850, 450);
73 }
74
75 void onOnceBeforeDraw() override {
76 const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100);
77 SkPictureRecorder recorder;
78 draw_something(recorder.beginRecording(bounds), bounds);
reedca2622b2016-03-18 07:25:55 -070079 fPicture = recorder.finishRecordingAsPicture();
reedd5b75632015-08-13 09:37:45 -070080
81 // extract enough just for the oval.
82 const SkISize size = SkISize::Make(100, 100);
Matt Sarett77a7a1b2017-02-07 13:56:11 -050083 auto srgbColorSpace = SkColorSpace::MakeSRGB();
reedd5b75632015-08-13 09:37:45 -070084
85 SkMatrix matrix;
86 matrix.setTranslate(-100, -100);
Matt Sarette94255d2017-01-09 12:38:59 -050087 fImage0 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr,
88 SkImage::BitDepth::kU8, srgbColorSpace);
reedd5b75632015-08-13 09:37:45 -070089 matrix.postTranslate(-50, -50);
90 matrix.postRotate(45);
91 matrix.postTranslate(50, 50);
Matt Sarette94255d2017-01-09 12:38:59 -050092 fImage1 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr,
93 SkImage::BitDepth::kU8, srgbColorSpace);
reedd5b75632015-08-13 09:37:45 -070094 }
95
96 void drawSet(SkCanvas* canvas) const {
97 SkMatrix matrix = SkMatrix::MakeTrans(-100, -100);
98 canvas->drawPicture(fPicture, &matrix, nullptr);
reed9ce9d672016-03-17 10:51:11 -070099 canvas->drawImage(fImage0.get(), 150, 0);
100 canvas->drawImage(fImage1.get(), 300, 0);
reedd5b75632015-08-13 09:37:45 -0700101 }
102
103 void onDraw(SkCanvas* canvas) override {
104 canvas->translate(20, 20);
105
106 this->drawSet(canvas);
107
108 canvas->save();
109 canvas->translate(0, 130);
110 canvas->scale(0.25f, 0.25f);
111 this->drawSet(canvas);
112 canvas->restore();
113
114 canvas->save();
115 canvas->translate(0, 200);
116 canvas->scale(2, 2);
117 this->drawSet(canvas);
118 canvas->restore();
119 }
120
121private:
122 typedef skiagm::GM INHERITED;
123};
124DEF_GM( return new ImagePictGM; )
125
reed8f343722015-08-13 13:32:39 -0700126///////////////////////////////////////////////////////////////////////////////////////////////////
127
Mike Reed185130c2017-02-15 15:14:16 -0500128static std::unique_ptr<SkImageGenerator> make_pic_generator(GrContext*, sk_sp<SkPicture> pic) {
reed935d6cf2015-08-18 11:16:09 -0700129 SkMatrix matrix;
130 matrix.setTranslate(-100, -100);
Mike Reed185130c2017-02-15 15:14:16 -0500131 return SkImageGenerator::MakeFromPicture({ 100, 100 }, std::move(pic), &matrix, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -0500132 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500133 SkColorSpace::MakeSRGB());
reed935d6cf2015-08-18 11:16:09 -0700134}
135
136class RasterGenerator : public SkImageGenerator {
137public:
Mike Reed4edb5d22017-04-17 11:02:51 -0400138 RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm)
139 {}
140
reed935d6cf2015-08-18 11:16:09 -0700141protected:
142 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Matt Sarettebb1b5c2017-05-12 11:41:27 -0400143 const Options&) override {
reedc4a83e22015-09-11 11:47:27 -0700144 SkASSERT(fBM.width() == info.width());
145 SkASSERT(fBM.height() == info.height());
Matt Sarettebb1b5c2017-05-12 11:41:27 -0400146 return fBM.readPixels(info, pixels, rowBytes, 0, 0);
reed935d6cf2015-08-18 11:16:09 -0700147 }
148private:
149 SkBitmap fBM;
150};
Mike Reed185130c2017-02-15 15:14:16 -0500151static std::unique_ptr<SkImageGenerator> make_ras_generator(GrContext*, sk_sp<SkPicture> pic) {
reed935d6cf2015-08-18 11:16:09 -0700152 SkBitmap bm;
153 bm.allocN32Pixels(100, 100);
154 SkCanvas canvas(bm);
155 canvas.clear(0);
156 canvas.translate(-100, -100);
157 canvas.drawPicture(pic);
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500158 return std::make_unique<RasterGenerator>(bm);
reed935d6cf2015-08-18 11:16:09 -0700159}
160
161class EmptyGenerator : public SkImageGenerator {
162public:
163 EmptyGenerator(const SkImageInfo& info) : SkImageGenerator(info) {}
164};
165
reed935d6cf2015-08-18 11:16:09 -0700166class TextureGenerator : public SkImageGenerator {
167public:
Mike Reed185130c2017-02-15 15:14:16 -0500168 TextureGenerator(GrContext* ctx, const SkImageInfo& info, sk_sp<SkPicture> pic)
reed935d6cf2015-08-18 11:16:09 -0700169 : SkImageGenerator(info)
Robert Phillipse2f7d182016-12-15 09:23:05 -0500170 , fCtx(SkRef(ctx)) {
171
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400172 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, info, 0,
Robert Phillipse44ef102017-07-21 15:37:19 -0400173 kTopLeft_GrSurfaceOrigin, nullptr));
scroggoe6f0d6e2016-05-13 07:25:44 -0700174 if (surface) {
175 surface->getCanvas()->clear(0);
176 surface->getCanvas()->translate(-100, -100);
177 surface->getCanvas()->drawPicture(pic);
178 sk_sp<SkImage> image(surface->makeImageSnapshot());
Robert Phillips6603a172019-03-05 12:35:44 -0500179 fProxy = as_IB(image)->asTextureProxyRef(fCtx.get());
scroggoe6f0d6e2016-05-13 07:25:44 -0700180 }
reed935d6cf2015-08-18 11:16:09 -0700181 }
182protected:
Robert Phillips9338c602019-02-19 12:52:29 -0500183 sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext* ctx, const SkImageInfo& info,
Christopher Cameron77e96662017-07-08 01:47:47 -0700184 const SkIPoint& origin,
Greg Danielf88c12e2017-10-09 09:57:35 -0400185 bool willBeMipped) override {
Robert Phillips4447b642017-03-03 11:10:18 -0500186 SkASSERT(ctx);
187 SkASSERT(ctx == fCtx.get());
reed935d6cf2015-08-18 11:16:09 -0700188
Robert Phillipse2f7d182016-12-15 09:23:05 -0500189 if (!fProxy) {
scroggoe6f0d6e2016-05-13 07:25:44 -0700190 return nullptr;
191 }
192
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400193 if (origin.fX == 0 && origin.fY == 0 && info.dimensions() == fProxy->dimensions()) {
Robert Phillips4447b642017-03-03 11:10:18 -0500194 return fProxy;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500195 }
196
Greg Daniel65c7f662017-10-30 13:39:09 -0400197 GrMipMapped mipMapped = willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo;
198
Brian Salomonfc118442019-11-22 19:09:27 -0500199 return GrSurfaceProxy::Copy(
Greg Daniel3155f7f2020-01-16 16:54:26 -0500200 fCtx.get(), fProxy.get(), SkColorTypeToGrColorType(info.colorType()), mipMapped,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400201 SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()),
202 SkBackingFit::kExact, SkBudgeted::kYes);
reed935d6cf2015-08-18 11:16:09 -0700203 }
Robert Phillips4447b642017-03-03 11:10:18 -0500204
reed935d6cf2015-08-18 11:16:09 -0700205private:
Robert Phillipse2f7d182016-12-15 09:23:05 -0500206 sk_sp<GrContext> fCtx;
Robert Phillips4447b642017-03-03 11:10:18 -0500207 sk_sp<GrTextureProxy> fProxy;
reed935d6cf2015-08-18 11:16:09 -0700208};
Robert Phillips4447b642017-03-03 11:10:18 -0500209
Mike Reed185130c2017-02-15 15:14:16 -0500210static std::unique_ptr<SkImageGenerator> make_tex_generator(GrContext* ctx, sk_sp<SkPicture> pic) {
reed935d6cf2015-08-18 11:16:09 -0700211 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
212
213 if (!ctx) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500214 return std::make_unique<EmptyGenerator>(info);
reed935d6cf2015-08-18 11:16:09 -0700215 }
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500216 return std::make_unique<TextureGenerator>(ctx, info, pic);
reed935d6cf2015-08-18 11:16:09 -0700217}
reed8f343722015-08-13 13:32:39 -0700218
219class ImageCacheratorGM : public skiagm::GM {
reed935d6cf2015-08-18 11:16:09 -0700220 SkString fName;
Mike Reed185130c2017-02-15 15:14:16 -0500221 std::unique_ptr<SkImageGenerator> (*fFactory)(GrContext*, sk_sp<SkPicture>);
reedca2622b2016-03-18 07:25:55 -0700222 sk_sp<SkPicture> fPicture;
Brian Osmana28e2b02017-04-24 16:44:03 -0400223 sk_sp<SkImage> fImage;
224 sk_sp<SkImage> fImageSubset;
reed8f343722015-08-13 13:32:39 -0700225
226public:
Mike Reed185130c2017-02-15 15:14:16 -0500227 ImageCacheratorGM(const char suffix[],
228 std::unique_ptr<SkImageGenerator> (*factory)(GrContext*, sk_sp<SkPicture>))
reed935d6cf2015-08-18 11:16:09 -0700229 : fFactory(factory)
230 {
231 fName.printf("image-cacherator-from-%s", suffix);
232 }
reed8f343722015-08-13 13:32:39 -0700233
234protected:
235 SkString onShortName() override {
reed935d6cf2015-08-18 11:16:09 -0700236 return fName;
reed8f343722015-08-13 13:32:39 -0700237 }
238
239 SkISize onISize() override {
reeda32cc952015-08-19 06:07:29 -0700240 return SkISize::Make(960, 450);
reed8f343722015-08-13 13:32:39 -0700241 }
242
243 void onOnceBeforeDraw() override {
244 const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100);
245 SkPictureRecorder recorder;
246 draw_something(recorder.beginRecording(bounds), bounds);
reedca2622b2016-03-18 07:25:55 -0700247 fPicture = recorder.finishRecordingAsPicture();
reed935d6cf2015-08-18 11:16:09 -0700248 }
reed8f343722015-08-13 13:32:39 -0700249
reed935d6cf2015-08-18 11:16:09 -0700250 void makeCaches(GrContext* ctx) {
Mike Reed185130c2017-02-15 15:14:16 -0500251 auto gen = fFactory(ctx, fPicture);
Brian Osmana28e2b02017-04-24 16:44:03 -0400252 fImage = SkImage::MakeFromGenerator(std::move(gen));
reed935d6cf2015-08-18 11:16:09 -0700253
254 const SkIRect subset = SkIRect::MakeLTRB(50, 50, 100, 100);
255
Mike Reed185130c2017-02-15 15:14:16 -0500256 gen = fFactory(ctx, fPicture);
Brian Osmana28e2b02017-04-24 16:44:03 -0400257 fImageSubset = SkImage::MakeFromGenerator(std::move(gen), &subset);
reed935d6cf2015-08-18 11:16:09 -0700258
Brian Osmandf7e0752017-04-26 16:20:28 -0400259 SkASSERT(fImage->dimensions() == SkISize::Make(100, 100));
260 SkASSERT(fImageSubset->dimensions() == SkISize::Make(50, 50));
reed935d6cf2015-08-18 11:16:09 -0700261 }
262
Brian Osmana28e2b02017-04-24 16:44:03 -0400263 static void draw_as_bitmap(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) {
reed935d6cf2015-08-18 11:16:09 -0700264 SkBitmap bitmap;
Brian Osmane50cdf02018-10-19 13:02:14 -0400265 as_IB(image)->getROPixels(&bitmap);
reed935d6cf2015-08-18 11:16:09 -0700266 canvas->drawBitmap(bitmap, x, y);
267 }
268
Brian Osmana28e2b02017-04-24 16:44:03 -0400269 static void draw_as_tex(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) {
Brian Salomon729fc0c2019-09-30 16:33:11 +0000270 sk_sp<GrTextureProxy> proxy(as_IB(image)->asTextureProxyRef(
Brian Salomonccb61422020-01-09 10:46:36 -0500271 canvas->getGrContext(), GrSamplerState::Filter::kBilerp, nullptr));
Brian Salomon729fc0c2019-09-30 16:33:11 +0000272 if (!proxy) {
reeda32cc952015-08-19 06:07:29 -0700273 // show placeholder if we have no texture
274 SkPaint paint;
275 paint.setStyle(SkPaint::kStroke_Style);
Brian Osmana28e2b02017-04-24 16:44:03 -0400276 SkRect r = SkRect::MakeXYWH(x, y, SkIntToScalar(image->width()),
277 SkIntToScalar(image->width()));
reeda32cc952015-08-19 06:07:29 -0700278 canvas->drawRect(r, paint);
279 canvas->drawLine(r.left(), r.top(), r.right(), r.bottom(), paint);
280 canvas->drawLine(r.left(), r.bottom(), r.right(), r.top(), paint);
reed935d6cf2015-08-18 11:16:09 -0700281 return;
282 }
Brian Salomon729fc0c2019-09-30 16:33:11 +0000283
Greg Daniel81b98972019-12-13 11:09:43 -0500284 // TODO: The asTextureProxyRef which takes a sampler and adjust needs to return a
285 // GrSurfaceProxyView instead. For now we just grab the info off the proxy.
286 GrSurfaceOrigin origin = proxy->origin();
287 const GrSwizzle& swizzle = proxy->textureSwizzle();
288 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
289
Brian Salomon729fc0c2019-09-30 16:33:11 +0000290 // No API to draw a GrTexture directly, so we cheat and create a private image subclass
291 sk_sp<SkImage> texImage(new SkImage_Gpu(sk_ref_sp(canvas->getGrContext()),
Greg Daniel7c165a42020-01-22 12:22:36 -0500292 image->uniqueID(), std::move(view),
293 image->colorType(), image->alphaType(),
294 image->refColorSpace()));
Brian Osmana28e2b02017-04-24 16:44:03 -0400295 canvas->drawImage(texImage.get(), x, y);
reed8f343722015-08-13 13:32:39 -0700296 }
297
298 void drawSet(SkCanvas* canvas) const {
299 SkMatrix matrix = SkMatrix::MakeTrans(-100, -100);
300 canvas->drawPicture(fPicture, &matrix, nullptr);
301
reed935d6cf2015-08-18 11:16:09 -0700302 // Draw the tex first, so it doesn't hit a lucky cache from the raster version. This
303 // way we also can force the generateTexture call.
304
Brian Osmana28e2b02017-04-24 16:44:03 -0400305 draw_as_tex(canvas, fImage.get(), 310, 0);
306 draw_as_tex(canvas, fImageSubset.get(), 310+101, 0);
reed935d6cf2015-08-18 11:16:09 -0700307
Brian Osmana28e2b02017-04-24 16:44:03 -0400308 draw_as_bitmap(canvas, fImage.get(), 150, 0);
309 draw_as_bitmap(canvas, fImageSubset.get(), 150+101, 0);
reed8f343722015-08-13 13:32:39 -0700310 }
311
312 void onDraw(SkCanvas* canvas) override {
reed935d6cf2015-08-18 11:16:09 -0700313 this->makeCaches(canvas->getGrContext());
314
reed8f343722015-08-13 13:32:39 -0700315 canvas->translate(20, 20);
316
317 this->drawSet(canvas);
318
319 canvas->save();
320 canvas->translate(0, 130);
321 canvas->scale(0.25f, 0.25f);
322 this->drawSet(canvas);
323 canvas->restore();
324
325 canvas->save();
326 canvas->translate(0, 200);
327 canvas->scale(2, 2);
328 this->drawSet(canvas);
329 canvas->restore();
330 }
331
332private:
333 typedef skiagm::GM INHERITED;
334};
reed935d6cf2015-08-18 11:16:09 -0700335DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator); )
336DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator); )
Brian Osmanc7ad40f2018-05-31 14:27:17 -0400337DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator); )