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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkImage.h" |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 11 | #include "SkImageGenerator.h" |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 12 | #include "SkImage_Base.h" |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 13 | #include "SkMakeUnique.h" |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 14 | #include "SkPictureRecorder.h" |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 15 | #include "SkSurface.h" |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 16 | |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 17 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 18 | #include "GrContextPriv.h" |
| 19 | #include "GrSurfaceContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 20 | #include "GrTextureProxy.h" |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 21 | #include "../src/image/SkImage_Gpu.h" |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 22 | |
| 23 | static void draw_something(SkCanvas* canvas, const SkRect& bounds) { |
| 24 | SkPaint paint; |
| 25 | paint.setAntiAlias(true); |
| 26 | paint.setColor(SK_ColorRED); |
| 27 | paint.setStyle(SkPaint::kStroke_Style); |
| 28 | paint.setStrokeWidth(10); |
| 29 | canvas->drawRect(bounds, paint); |
| 30 | paint.setStyle(SkPaint::kFill_Style); |
| 31 | paint.setColor(SK_ColorBLUE); |
| 32 | canvas->drawOval(bounds, paint); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Exercise drawing pictures inside an image, showing that the image version is pixelated |
| 37 | * (correctly) when it is inside an image. |
| 38 | */ |
| 39 | class ImagePictGM : public skiagm::GM { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 40 | sk_sp<SkPicture> fPicture; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 41 | sk_sp<SkImage> fImage0; |
| 42 | sk_sp<SkImage> fImage1; |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 43 | public: |
| 44 | ImagePictGM() {} |
| 45 | |
| 46 | protected: |
| 47 | SkString onShortName() override { |
| 48 | return SkString("image-picture"); |
| 49 | } |
| 50 | |
| 51 | SkISize onISize() override { |
| 52 | return SkISize::Make(850, 450); |
| 53 | } |
| 54 | |
| 55 | void onOnceBeforeDraw() override { |
| 56 | const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100); |
| 57 | SkPictureRecorder recorder; |
| 58 | draw_something(recorder.beginRecording(bounds), bounds); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 59 | fPicture = recorder.finishRecordingAsPicture(); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 60 | |
| 61 | // extract enough just for the oval. |
| 62 | const SkISize size = SkISize::Make(100, 100); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 63 | auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 64 | |
| 65 | SkMatrix matrix; |
| 66 | matrix.setTranslate(-100, -100); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 67 | fImage0 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr, |
| 68 | SkImage::BitDepth::kU8, srgbColorSpace); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 69 | matrix.postTranslate(-50, -50); |
| 70 | matrix.postRotate(45); |
| 71 | matrix.postTranslate(50, 50); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 72 | fImage1 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr, |
| 73 | SkImage::BitDepth::kU8, srgbColorSpace); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void drawSet(SkCanvas* canvas) const { |
| 77 | SkMatrix matrix = SkMatrix::MakeTrans(-100, -100); |
| 78 | canvas->drawPicture(fPicture, &matrix, nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 79 | canvas->drawImage(fImage0.get(), 150, 0); |
| 80 | canvas->drawImage(fImage1.get(), 300, 0); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void onDraw(SkCanvas* canvas) override { |
| 84 | canvas->translate(20, 20); |
| 85 | |
| 86 | this->drawSet(canvas); |
| 87 | |
| 88 | canvas->save(); |
| 89 | canvas->translate(0, 130); |
| 90 | canvas->scale(0.25f, 0.25f); |
| 91 | this->drawSet(canvas); |
| 92 | canvas->restore(); |
| 93 | |
| 94 | canvas->save(); |
| 95 | canvas->translate(0, 200); |
| 96 | canvas->scale(2, 2); |
| 97 | this->drawSet(canvas); |
| 98 | canvas->restore(); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | typedef skiagm::GM INHERITED; |
| 103 | }; |
| 104 | DEF_GM( return new ImagePictGM; ) |
| 105 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 107 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 108 | static std::unique_ptr<SkImageGenerator> make_pic_generator(GrContext*, sk_sp<SkPicture> pic) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 109 | SkMatrix matrix; |
| 110 | matrix.setTranslate(-100, -100); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 111 | return SkImageGenerator::MakeFromPicture({ 100, 100 }, std::move(pic), &matrix, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 112 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 113 | SkColorSpace::MakeSRGB()); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | class RasterGenerator : public SkImageGenerator { |
| 117 | public: |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 118 | RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) |
| 119 | {} |
| 120 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 121 | protected: |
| 122 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 123 | const Options&) override { |
reed | c4a83e2 | 2015-09-11 11:47:27 -0700 | [diff] [blame] | 124 | SkASSERT(fBM.width() == info.width()); |
| 125 | SkASSERT(fBM.height() == info.height()); |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 126 | return fBM.readPixels(info, pixels, rowBytes, 0, 0); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 127 | } |
| 128 | private: |
| 129 | SkBitmap fBM; |
| 130 | }; |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 131 | static std::unique_ptr<SkImageGenerator> make_ras_generator(GrContext*, sk_sp<SkPicture> pic) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 132 | SkBitmap bm; |
| 133 | bm.allocN32Pixels(100, 100); |
| 134 | SkCanvas canvas(bm); |
| 135 | canvas.clear(0); |
| 136 | canvas.translate(-100, -100); |
| 137 | canvas.drawPicture(pic); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 138 | return skstd::make_unique<RasterGenerator>(bm); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | class EmptyGenerator : public SkImageGenerator { |
| 142 | public: |
| 143 | EmptyGenerator(const SkImageInfo& info) : SkImageGenerator(info) {} |
| 144 | }; |
| 145 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 146 | class TextureGenerator : public SkImageGenerator { |
| 147 | public: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 148 | TextureGenerator(GrContext* ctx, const SkImageInfo& info, sk_sp<SkPicture> pic) |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 149 | : SkImageGenerator(info) |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 150 | , fCtx(SkRef(ctx)) { |
| 151 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 152 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, info, 0, |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 153 | kTopLeft_GrSurfaceOrigin, nullptr)); |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 154 | if (surface) { |
| 155 | surface->getCanvas()->clear(0); |
| 156 | surface->getCanvas()->translate(-100, -100); |
| 157 | surface->getCanvas()->drawPicture(pic); |
| 158 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 159 | fProxy = as_IB(image)->asTextureProxyRef(); |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 160 | } |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 161 | } |
| 162 | protected: |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 163 | sk_sp<GrTextureProxy> onGenerateTexture(GrContext* ctx, const SkImageInfo& info, |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 164 | const SkIPoint& origin, |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 165 | SkTransferFunctionBehavior, |
| 166 | bool willBeMipped) override { |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 167 | SkASSERT(ctx); |
| 168 | SkASSERT(ctx == fCtx.get()); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 169 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 170 | if (!fProxy) { |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 171 | return nullptr; |
| 172 | } |
| 173 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 174 | if (origin.fX == 0 && origin.fY == 0 && |
| 175 | info.width() == fProxy->width() && info.height() == fProxy->height()) { |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 176 | return fProxy; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 177 | } |
| 178 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 179 | // need to copy the subset into a new texture |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 180 | GrSurfaceDesc desc; |
Brian Osman | 222e9ad | 2016-12-14 15:42:36 -0500 | [diff] [blame] | 181 | desc.fWidth = info.width(); |
| 182 | desc.fHeight = info.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 183 | desc.fConfig = fProxy->config(); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 184 | |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 185 | GrMipMapped mipMapped = willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 186 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 187 | sk_sp<GrSurfaceContext> dstContext(fCtx->contextPriv().makeDeferredSurfaceContext( |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 188 | desc, fProxy->origin(), mipMapped, SkBackingFit::kExact, SkBudgeted::kYes)); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 189 | if (!dstContext) { |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | if (!dstContext->copy( |
| 194 | fProxy.get(), |
| 195 | SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()), |
| 196 | SkIPoint::Make(0, 0))) { |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 200 | return dstContext->asTextureProxyRef(); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 201 | } |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 202 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 203 | private: |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 204 | sk_sp<GrContext> fCtx; |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 205 | sk_sp<GrTextureProxy> fProxy; |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 206 | }; |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 207 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 208 | 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] | 209 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 210 | |
| 211 | if (!ctx) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 212 | return skstd::make_unique<EmptyGenerator>(info); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 213 | } |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 214 | return skstd::make_unique<TextureGenerator>(ctx, info, pic); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 215 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 216 | |
| 217 | class ImageCacheratorGM : public skiagm::GM { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 218 | SkString fName; |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 219 | std::unique_ptr<SkImageGenerator> (*fFactory)(GrContext*, sk_sp<SkPicture>); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 220 | sk_sp<SkPicture> fPicture; |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 221 | sk_sp<SkImage> fImage; |
| 222 | sk_sp<SkImage> fImageSubset; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 223 | |
| 224 | public: |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 225 | ImageCacheratorGM(const char suffix[], |
| 226 | std::unique_ptr<SkImageGenerator> (*factory)(GrContext*, sk_sp<SkPicture>)) |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 227 | : fFactory(factory) |
| 228 | { |
| 229 | fName.printf("image-cacherator-from-%s", suffix); |
| 230 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 231 | |
| 232 | protected: |
| 233 | SkString onShortName() override { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 234 | return fName; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | SkISize onISize() override { |
reed | a32cc95 | 2015-08-19 06:07:29 -0700 | [diff] [blame] | 238 | return SkISize::Make(960, 450); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void onOnceBeforeDraw() override { |
| 242 | const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100); |
| 243 | SkPictureRecorder recorder; |
| 244 | draw_something(recorder.beginRecording(bounds), bounds); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 245 | fPicture = recorder.finishRecordingAsPicture(); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 246 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 247 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 248 | void makeCaches(GrContext* ctx) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 249 | auto gen = fFactory(ctx, fPicture); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 250 | fImage = SkImage::MakeFromGenerator(std::move(gen)); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 251 | |
| 252 | const SkIRect subset = SkIRect::MakeLTRB(50, 50, 100, 100); |
| 253 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 254 | gen = fFactory(ctx, fPicture); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 255 | fImageSubset = SkImage::MakeFromGenerator(std::move(gen), &subset); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 256 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 257 | SkASSERT(fImage->dimensions() == SkISize::Make(100, 100)); |
| 258 | SkASSERT(fImageSubset->dimensions() == SkISize::Make(50, 50)); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 261 | static void draw_as_bitmap(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 262 | SkBitmap bitmap; |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 263 | as_IB(image)->getROPixels(&bitmap, canvas->imageInfo().colorSpace()); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 264 | canvas->drawBitmap(bitmap, x, y); |
| 265 | } |
| 266 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 267 | static void draw_as_tex(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) { |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 268 | sk_sp<SkColorSpace> texColorSpace; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 269 | sk_sp<GrTextureProxy> proxy(as_IB(image)->asTextureProxyRef( |
| 270 | canvas->getGrContext(), GrSamplerState::ClampBilerp(), |
| 271 | canvas->imageInfo().colorSpace(), &texColorSpace, nullptr)); |
Robert Phillips | 4f358be | 2017-03-23 08:21:00 -0400 | [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 | } |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 283 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 284 | // No API to draw a GrTexture directly, so we cheat and create a private image subclass |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 285 | sk_sp<SkImage> texImage(new SkImage_Gpu( |
| 286 | sk_ref_sp(canvas->getGrContext()), image->uniqueID(), kPremul_SkAlphaType, |
| 287 | std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo)); |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 288 | canvas->drawImage(texImage.get(), x, y); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void drawSet(SkCanvas* canvas) const { |
| 292 | SkMatrix matrix = SkMatrix::MakeTrans(-100, -100); |
| 293 | canvas->drawPicture(fPicture, &matrix, nullptr); |
| 294 | |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 295 | // Draw the tex first, so it doesn't hit a lucky cache from the raster version. This |
| 296 | // way we also can force the generateTexture call. |
| 297 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 298 | draw_as_tex(canvas, fImage.get(), 310, 0); |
| 299 | draw_as_tex(canvas, fImageSubset.get(), 310+101, 0); |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 300 | |
Brian Osman | a28e2b0 | 2017-04-24 16:44:03 -0400 | [diff] [blame] | 301 | draw_as_bitmap(canvas, fImage.get(), 150, 0); |
| 302 | draw_as_bitmap(canvas, fImageSubset.get(), 150+101, 0); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void onDraw(SkCanvas* canvas) override { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 306 | this->makeCaches(canvas->getGrContext()); |
| 307 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 308 | canvas->translate(20, 20); |
| 309 | |
| 310 | this->drawSet(canvas); |
| 311 | |
| 312 | canvas->save(); |
| 313 | canvas->translate(0, 130); |
| 314 | canvas->scale(0.25f, 0.25f); |
| 315 | this->drawSet(canvas); |
| 316 | canvas->restore(); |
| 317 | |
| 318 | canvas->save(); |
| 319 | canvas->translate(0, 200); |
| 320 | canvas->scale(2, 2); |
| 321 | this->drawSet(canvas); |
| 322 | canvas->restore(); |
| 323 | } |
| 324 | |
| 325 | private: |
| 326 | typedef skiagm::GM INHERITED; |
| 327 | }; |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 328 | DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator); ) |
| 329 | DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator); ) |
Brian Osman | c7ad40f | 2018-05-31 14:27:17 -0400 | [diff] [blame] | 330 | DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator); ) |