reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorSpace.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkImage.h" |
| 14 | #include "include/core/SkImageGenerator.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkMatrix.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkPicture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 29 | #include "include/gpu/GrTypes.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 32 | #include "src/gpu/GrSamplerState.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 34 | #include "src/gpu/GrTextureProxy.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 35 | #include "src/image/SkImage_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/image/SkImage_Gpu.h" |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 37 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 38 | #include <memory> |
| 39 | #include <utility> |
| 40 | |
| 41 | class GrRecordingContext; |
| 42 | |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 43 | static 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 | */ |
| 59 | class ImagePictGM : public skiagm::GM { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 60 | sk_sp<SkPicture> fPicture; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 61 | sk_sp<SkImage> fImage0; |
| 62 | sk_sp<SkImage> fImage1; |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 63 | public: |
| 64 | ImagePictGM() {} |
| 65 | |
| 66 | protected: |
| 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); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 79 | fPicture = recorder.finishRecordingAsPicture(); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 80 | |
| 81 | // extract enough just for the oval. |
| 82 | const SkISize size = SkISize::Make(100, 100); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 83 | auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 84 | |
| 85 | SkMatrix matrix; |
| 86 | matrix.setTranslate(-100, -100); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 87 | fImage0 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr, |
| 88 | SkImage::BitDepth::kU8, srgbColorSpace); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 89 | matrix.postTranslate(-50, -50); |
| 90 | matrix.postRotate(45); |
| 91 | matrix.postTranslate(50, 50); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 92 | fImage1 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr, |
| 93 | SkImage::BitDepth::kU8, srgbColorSpace); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void drawSet(SkCanvas* canvas) const { |
| 97 | SkMatrix matrix = SkMatrix::MakeTrans(-100, -100); |
| 98 | canvas->drawPicture(fPicture, &matrix, nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 99 | canvas->drawImage(fImage0.get(), 150, 0); |
| 100 | canvas->drawImage(fImage1.get(), 300, 0); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 101 | } |
| 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 | |
| 121 | private: |
| 122 | typedef skiagm::GM INHERITED; |
| 123 | }; |
| 124 | DEF_GM( return new ImagePictGM; ) |
| 125 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 126 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 127 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 128 | static std::unique_ptr<SkImageGenerator> make_pic_generator(GrContext*, sk_sp<SkPicture> pic) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 129 | SkMatrix matrix; |
| 130 | matrix.setTranslate(-100, -100); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 131 | return SkImageGenerator::MakeFromPicture({ 100, 100 }, std::move(pic), &matrix, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 132 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 133 | SkColorSpace::MakeSRGB()); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | class RasterGenerator : public SkImageGenerator { |
| 137 | public: |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 138 | RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) |
| 139 | {} |
| 140 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 141 | protected: |
| 142 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 143 | const Options&) override { |
reed | c4a83e2 | 2015-09-11 11:47:27 -0700 | [diff] [blame] | 144 | SkASSERT(fBM.width() == info.width()); |
| 145 | SkASSERT(fBM.height() == info.height()); |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 146 | return fBM.readPixels(info, pixels, rowBytes, 0, 0); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 147 | } |
| 148 | private: |
| 149 | SkBitmap fBM; |
| 150 | }; |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 151 | static std::unique_ptr<SkImageGenerator> make_ras_generator(GrContext*, sk_sp<SkPicture> pic) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 152 | 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 Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 158 | return std::make_unique<RasterGenerator>(bm); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | class EmptyGenerator : public SkImageGenerator { |
| 162 | public: |
| 163 | EmptyGenerator(const SkImageInfo& info) : SkImageGenerator(info) {} |
| 164 | }; |
| 165 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 166 | class TextureGenerator : public SkImageGenerator { |
| 167 | public: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 168 | TextureGenerator(GrContext* ctx, const SkImageInfo& info, sk_sp<SkPicture> pic) |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 169 | : SkImageGenerator(info) |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 170 | , fCtx(SkRef(ctx)) { |
| 171 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 172 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, info, 0, |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 173 | kTopLeft_GrSurfaceOrigin, nullptr)); |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 174 | 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 Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 179 | fProxy = as_IB(image)->asTextureProxyRef(fCtx.get()); |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 180 | } |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 181 | } |
| 182 | protected: |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 183 | sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext* ctx, const SkImageInfo& info, |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 184 | const SkIPoint& origin, |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 185 | bool willBeMipped) override { |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 186 | SkASSERT(ctx); |
| 187 | SkASSERT(ctx == fCtx.get()); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 188 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 189 | if (!fProxy) { |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 190 | return nullptr; |
| 191 | } |
| 192 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 193 | if (origin.fX == 0 && origin.fY == 0 && info.dimensions() == fProxy->dimensions()) { |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 194 | return fProxy; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 197 | GrMipMapped mipMapped = willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 198 | |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 199 | return GrSurfaceProxy::Copy( |
Greg Daniel | 3155f7f | 2020-01-16 16:54:26 -0500 | [diff] [blame] | 200 | fCtx.get(), fProxy.get(), SkColorTypeToGrColorType(info.colorType()), mipMapped, |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 201 | SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()), |
| 202 | SkBackingFit::kExact, SkBudgeted::kYes); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 203 | } |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 204 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 205 | private: |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 206 | sk_sp<GrContext> fCtx; |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 207 | sk_sp<GrTextureProxy> fProxy; |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 208 | }; |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 209 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 210 | static std::unique_ptr<SkImageGenerator> make_tex_generator(GrContext* ctx, sk_sp<SkPicture> pic) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 211 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 212 | |
| 213 | if (!ctx) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 214 | return std::make_unique<EmptyGenerator>(info); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 215 | } |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 216 | return std::make_unique<TextureGenerator>(ctx, info, pic); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 217 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 218 | |
| 219 | class ImageCacheratorGM : public skiagm::GM { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 220 | SkString fName; |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 221 | std::unique_ptr<SkImageGenerator> (*fFactory)(GrContext*, sk_sp<SkPicture>); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 222 | sk_sp<SkPicture> fPicture; |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 223 | sk_sp<SkImage> fImage; |
| 224 | sk_sp<SkImage> fImageSubset; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 225 | |
| 226 | public: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 227 | ImageCacheratorGM(const char suffix[], |
| 228 | std::unique_ptr<SkImageGenerator> (*factory)(GrContext*, sk_sp<SkPicture>)) |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 229 | : fFactory(factory) |
| 230 | { |
| 231 | fName.printf("image-cacherator-from-%s", suffix); |
| 232 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 233 | |
| 234 | protected: |
| 235 | SkString onShortName() override { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 236 | return fName; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | SkISize onISize() override { |
reed | a32cc95 | 2015-08-19 06:07:29 -0700 | [diff] [blame] | 240 | return SkISize::Make(960, 450); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 241 | } |
| 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); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 247 | fPicture = recorder.finishRecordingAsPicture(); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 248 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 249 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 250 | void makeCaches(GrContext* ctx) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 251 | auto gen = fFactory(ctx, fPicture); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 252 | fImage = SkImage::MakeFromGenerator(std::move(gen)); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 253 | |
| 254 | const SkIRect subset = SkIRect::MakeLTRB(50, 50, 100, 100); |
| 255 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 256 | gen = fFactory(ctx, fPicture); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 257 | fImageSubset = SkImage::MakeFromGenerator(std::move(gen), &subset); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 258 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 259 | SkASSERT(fImage->dimensions() == SkISize::Make(100, 100)); |
| 260 | SkASSERT(fImageSubset->dimensions() == SkISize::Make(50, 50)); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 263 | static void draw_as_bitmap(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 264 | SkBitmap bitmap; |
Brian Osman | e50cdf0 | 2018-10-19 13:02:14 -0400 | [diff] [blame] | 265 | as_IB(image)->getROPixels(&bitmap); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 266 | canvas->drawBitmap(bitmap, x, y); |
| 267 | } |
| 268 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 269 | static void draw_as_tex(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) { |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 270 | sk_sp<GrTextureProxy> proxy(as_IB(image)->asTextureProxyRef( |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 271 | canvas->getGrContext(), GrSamplerState::Filter::kBilerp, nullptr)); |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 272 | if (!proxy) { |
reed | a32cc95 | 2015-08-19 06:07:29 -0700 | [diff] [blame] | 273 | // show placeholder if we have no texture |
| 274 | SkPaint paint; |
| 275 | paint.setStyle(SkPaint::kStroke_Style); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 276 | SkRect r = SkRect::MakeXYWH(x, y, SkIntToScalar(image->width()), |
| 277 | SkIntToScalar(image->width())); |
reed | a32cc95 | 2015-08-19 06:07:29 -0700 | [diff] [blame] | 278 | 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); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
Brian Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 283 | |
Greg Daniel | 81b9897 | 2019-12-13 11:09:43 -0500 | [diff] [blame] | 284 | // 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 Salomon | 729fc0c | 2019-09-30 16:33:11 +0000 | [diff] [blame] | 290 | // 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 Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 292 | image->uniqueID(), std::move(view), |
| 293 | image->colorType(), image->alphaType(), |
| 294 | image->refColorSpace())); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 295 | canvas->drawImage(texImage.get(), x, y); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void drawSet(SkCanvas* canvas) const { |
| 299 | SkMatrix matrix = SkMatrix::MakeTrans(-100, -100); |
| 300 | canvas->drawPicture(fPicture, &matrix, nullptr); |
| 301 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 302 | // 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 Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 305 | draw_as_tex(canvas, fImage.get(), 310, 0); |
| 306 | draw_as_tex(canvas, fImageSubset.get(), 310+101, 0); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 307 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 308 | draw_as_bitmap(canvas, fImage.get(), 150, 0); |
| 309 | draw_as_bitmap(canvas, fImageSubset.get(), 150+101, 0); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void onDraw(SkCanvas* canvas) override { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 313 | this->makeCaches(canvas->getGrContext()); |
| 314 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 315 | 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 | |
| 332 | private: |
| 333 | typedef skiagm::GM INHERITED; |
| 334 | }; |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 335 | DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator); ) |
| 336 | DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator); ) |
Brian Osman | c7ad40f | 2018-05-31 14:27:17 -0400 | [diff] [blame] | 337 | DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator); ) |