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