mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 8 | #include <functional> |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 9 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 10 | #include "sk_tool_utils.h" |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 11 | #include "SkAutoPixmapStorage.h" |
Mike Reed | 5dd202d | 2018-02-06 23:05:36 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 14 | #include "SkCanvas.h" |
| 15 | #include "SkRandom.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkSurface.h" |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 19 | #include "GrContext.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 20 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 21 | static void drawContents(SkSurface* surface, SkColor fillC) { |
skia.committer@gmail.com | 04ba448 | 2012-09-07 02:01:30 +0000 | [diff] [blame] | 22 | SkSize size = SkSize::Make(SkIntToScalar(surface->width()), |
robertphillips@google.com | 94acc70 | 2012-09-06 18:43:21 +0000 | [diff] [blame] | 23 | SkIntToScalar(surface->height())); |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 24 | SkCanvas* canvas = surface->getCanvas(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 25 | |
| 26 | SkScalar stroke = size.fWidth / 10; |
| 27 | SkScalar radius = (size.fWidth - stroke) / 2; |
| 28 | |
| 29 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 30 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 31 | paint.setAntiAlias(true); |
| 32 | paint.setColor(fillC); |
| 33 | canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 34 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 35 | paint.setStyle(SkPaint::kStroke_Style); |
| 36 | paint.setStrokeWidth(stroke); |
| 37 | paint.setColor(SK_ColorBLACK); |
| 38 | canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); |
| 39 | } |
| 40 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 41 | static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) { |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 42 | drawContents(surf, SK_ColorRED); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 43 | sk_sp<SkImage> imgR = surf->makeImageSnapshot(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 44 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 45 | if (true) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 46 | sk_sp<SkImage> imgR2 = surf->makeImageSnapshot(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 47 | SkASSERT(imgR == imgR2); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 48 | } |
| 49 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 50 | drawContents(surf, SK_ColorGREEN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 51 | sk_sp<SkImage> imgG = surf->makeImageSnapshot(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 52 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 53 | // since we've drawn after we snapped imgR, imgG will be a different obj |
| 54 | SkASSERT(imgR != imgG); |
| 55 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 56 | drawContents(surf, SK_ColorBLUE); |
| 57 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 58 | SkPaint paint; |
| 59 | // paint.setFilterBitmap(true); |
| 60 | // paint.setAlpha(0x80); |
| 61 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 62 | canvas->drawImage(imgR, 0, 0, usePaint ? &paint : nullptr); |
| 63 | canvas->drawImage(imgG, 0, 80, usePaint ? &paint : nullptr); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | surf->draw(canvas, 0, 160, usePaint ? &paint : nullptr); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 65 | |
| 66 | SkRect src1, src2, src3; |
| 67 | src1.iset(0, 0, surf->width(), surf->height()); |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 68 | src2.iset(-surf->width() / 2, -surf->height() / 2, |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 69 | surf->width(), surf->height()); |
| 70 | src3.iset(0, 0, surf->width() / 2, surf->height() / 2); |
| 71 | |
| 72 | SkRect dst1, dst2, dst3, dst4; |
| 73 | dst1.set(0, 240, 65, 305); |
| 74 | dst2.set(0, 320, 65, 385); |
| 75 | dst3.set(0, 400, 65, 465); |
| 76 | dst4.set(0, 480, 65, 545); |
| 77 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 78 | canvas->drawImageRect(imgR, src1, dst1, usePaint ? &paint : nullptr); |
| 79 | canvas->drawImageRect(imgG, src2, dst2, usePaint ? &paint : nullptr); |
| 80 | canvas->drawImageRect(imgR, src3, dst3, usePaint ? &paint : nullptr); |
| 81 | canvas->drawImageRect(imgG, dst4, usePaint ? &paint : nullptr); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | class ImageGM : public skiagm::GM { |
| 85 | void* fBuffer; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 86 | size_t fBufferSize; |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 87 | SkSize fSize; |
| 88 | enum { |
| 89 | W = 64, |
| 90 | H = 64, |
| 91 | RB = W * 4 + 8, |
| 92 | }; |
| 93 | public: |
| 94 | ImageGM() { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 95 | fBufferSize = RB * H; |
| 96 | fBuffer = sk_malloc_throw(fBufferSize); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 97 | fSize.set(SkIntToScalar(W), SkIntToScalar(H)); |
| 98 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 100 | ~ImageGM() override { |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 101 | sk_free(fBuffer); |
| 102 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 104 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 105 | SkString onShortName() override { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 106 | return SkString("image-surface"); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 107 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 108 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 109 | SkISize onISize() override { |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 110 | return SkISize::Make(960, 1200); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 111 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 114 | canvas->scale(2, 2); |
| 115 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 116 | const char* kLabel1 = "Original Img"; |
| 117 | const char* kLabel2 = "Modified Img"; |
| 118 | const char* kLabel3 = "Cur Surface"; |
| 119 | const char* kLabel4 = "Full Crop"; |
| 120 | const char* kLabel5 = "Over-crop"; |
| 121 | const char* kLabel6 = "Upper-left"; |
| 122 | const char* kLabel7 = "No Crop"; |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 123 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 124 | const char* kLabel8 = "Pre-Alloc Img"; |
| 125 | const char* kLabel9 = "New Alloc Img"; |
| 126 | const char* kLabel10 = "GPU"; |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 127 | |
| 128 | SkPaint textPaint; |
commit-bot@chromium.org | cae54f1 | 2014-04-11 18:34:35 +0000 | [diff] [blame] | 129 | textPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 130 | sk_tool_utils::set_portable_typeface(&textPaint); |
commit-bot@chromium.org | cae54f1 | 2014-04-11 18:34:35 +0000 | [diff] [blame] | 131 | textPaint.setTextSize(8); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 132 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 133 | canvas->drawString(kLabel1, 10, 60, textPaint); |
| 134 | canvas->drawString(kLabel2, 10, 140, textPaint); |
| 135 | canvas->drawString(kLabel3, 10, 220, textPaint); |
| 136 | canvas->drawString(kLabel4, 10, 300, textPaint); |
| 137 | canvas->drawString(kLabel5, 10, 380, textPaint); |
| 138 | canvas->drawString(kLabel6, 10, 460, textPaint); |
| 139 | canvas->drawString(kLabel7, 10, 540, textPaint); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 140 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 141 | canvas->drawString(kLabel8, 80, 10, textPaint); |
| 142 | canvas->drawString(kLabel9, 160, 10, textPaint); |
| 143 | canvas->drawString(kLabel10, 265, 10, textPaint); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 144 | |
| 145 | canvas->translate(80, 20); |
| 146 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 147 | // since we draw into this directly, we need to start fresh |
| 148 | sk_bzero(fBuffer, fBufferSize); |
| 149 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 150 | SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 151 | sk_sp<SkSurface> surf0(SkSurface::MakeRasterDirect(info, fBuffer, RB)); |
| 152 | sk_sp<SkSurface> surf1(SkSurface::MakeRaster(info)); |
Brian Osman | c7ad40f | 2018-05-31 14:27:17 -0400 | [diff] [blame] | 153 | sk_sp<SkSurface> surf2(SkSurface::MakeRenderTarget(canvas->getGrContext(), |
| 154 | SkBudgeted::kNo, info)); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 155 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 156 | test_surface(canvas, surf0.get(), true); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 157 | canvas->translate(80, 0); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 158 | test_surface(canvas, surf1.get(), true); |
reed | a9cb871 | 2015-01-16 14:21:40 -0800 | [diff] [blame] | 159 | if (surf2) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 160 | canvas->translate(80, 0); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 161 | test_surface(canvas, surf2.get(), true); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 162 | } |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 163 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 164 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 165 | private: |
| 166 | typedef skiagm::GM INHERITED; |
| 167 | }; |
reed | a9cb871 | 2015-01-16 14:21:40 -0800 | [diff] [blame] | 168 | DEF_GM( return new ImageGM; ) |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 169 | |
| 170 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 171 | |
| 172 | #include "SkPictureRecorder.h" |
| 173 | |
| 174 | static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) { |
| 175 | SkBitmap bitmap; |
halcanary | e36ec87 | 2015-12-09 11:36:59 -0800 | [diff] [blame] | 176 | bitmap.installPixels(pmap); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 177 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
| 178 | } |
| 179 | |
| 180 | static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) { |
| 181 | SkAutoCanvasRestore acr(canvas, true); |
| 182 | |
| 183 | canvas->drawImage(image, 0, 0, nullptr); |
| 184 | canvas->translate(110, 10); |
| 185 | |
| 186 | const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40); |
| 187 | SkAutoPixmapStorage storage; |
| 188 | storage.alloc(info); |
| 189 | |
| 190 | const SkImage::CachingHint chints[] = { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 191 | SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 192 | }; |
| 193 | const SkFilterQuality qualities[] = { |
| 194 | kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality, |
| 195 | }; |
| 196 | |
| 197 | for (auto ch : chints) { |
| 198 | canvas->save(); |
| 199 | for (auto q : qualities) { |
| 200 | if (image->scalePixels(storage, q, ch)) { |
| 201 | draw_pixmap(canvas, storage); |
| 202 | } |
| 203 | canvas->translate(70, 0); |
| 204 | } |
| 205 | canvas->restore(); |
| 206 | canvas->translate(0, 45); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void draw_contents(SkCanvas* canvas) { |
| 211 | SkPaint paint; |
| 212 | paint.setStyle(SkPaint::kStroke_Style); |
| 213 | paint.setStrokeWidth(20); |
| 214 | canvas->drawCircle(50, 50, 35, paint); |
| 215 | } |
| 216 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 217 | static sk_sp<SkImage> make_raster(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 218 | auto surface(SkSurface::MakeRaster(info)); |
reed | 7850eb2 | 2015-12-02 14:19:47 -0800 | [diff] [blame] | 219 | draw(surface->getCanvas()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 220 | return surface->makeImageSnapshot(); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 221 | } |
| 222 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 223 | static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 224 | SkPictureRecorder recorder; |
reed | 7850eb2 | 2015-12-02 14:19:47 -0800 | [diff] [blame] | 225 | draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height()))); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 226 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 227 | info.dimensions(), nullptr, nullptr, SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 228 | SkColorSpace::MakeSRGB()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 229 | } |
| 230 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 231 | static sk_sp<SkImage> make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) { |
| 232 | sk_sp<SkImage> image(make_raster(info, nullptr, draw)); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 233 | return SkImage::MakeFromEncoded(image->encodeToData()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 234 | } |
| 235 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 236 | static sk_sp<SkImage> make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 237 | if (!ctx) { return nullptr; } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 238 | auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info)); |
scroggo | e6f0d6e | 2016-05-13 07:25:44 -0700 | [diff] [blame] | 239 | if (!surface) { return nullptr; } |
reed | 7850eb2 | 2015-12-02 14:19:47 -0800 | [diff] [blame] | 240 | draw(surface->getCanvas()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 241 | return surface->makeImageSnapshot(); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 242 | } |
| 243 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 244 | typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&, GrContext*, void (*)(SkCanvas*)); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 245 | |
| 246 | class ScalePixelsGM : public skiagm::GM { |
| 247 | public: |
| 248 | ScalePixelsGM() {} |
| 249 | |
| 250 | protected: |
| 251 | SkString onShortName() override { |
| 252 | return SkString("scale-pixels"); |
| 253 | } |
| 254 | |
| 255 | SkISize onISize() override { |
| 256 | return SkISize::Make(960, 1200); |
| 257 | } |
| 258 | |
| 259 | void onDraw(SkCanvas* canvas) override { |
| 260 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 261 | |
| 262 | const ImageMakerProc procs[] = { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 263 | make_codec, make_raster, make_picture, make_codec, make_gpu, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 264 | }; |
| 265 | for (auto& proc : procs) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 266 | sk_sp<SkImage> image(proc(info, canvas->getGrContext(), draw_contents)); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 267 | if (image) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 268 | show_scaled_pixels(canvas, image.get()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 269 | } |
| 270 | canvas->translate(0, 120); |
| 271 | } |
| 272 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 273 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 274 | private: |
| 275 | typedef skiagm::GM INHERITED; |
| 276 | }; |
| 277 | DEF_GM( return new ScalePixelsGM; ) |
reed | 7850eb2 | 2015-12-02 14:19:47 -0800 | [diff] [blame] | 278 | |
| 279 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 280 | |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 281 | DEF_SIMPLE_GM(new_texture_image, canvas, 280, 60) { |
| 282 | GrContext* context = canvas->getGrContext(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 283 | if (!context) { |
| 284 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | auto render_image = [](SkCanvas* canvas) { |
| 289 | canvas->clear(SK_ColorBLUE); |
| 290 | SkPaint paint; |
| 291 | paint.setColor(SK_ColorRED); |
| 292 | canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint); |
| 293 | paint.setColor(SK_ColorGREEN); |
| 294 | canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint); |
| 295 | paint.setColor(SK_ColorYELLOW); |
| 296 | canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint); |
| 297 | paint.setColor(SK_ColorCYAN); |
| 298 | canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint); |
| 299 | }; |
| 300 | |
| 301 | static constexpr int kSize = 50; |
| 302 | SkBitmap bmp; |
| 303 | bmp.allocPixels(SkImageInfo::MakeS32(kSize, kSize, kPremul_SkAlphaType)); |
| 304 | SkCanvas bmpCanvas(bmp); |
| 305 | render_image(&bmpCanvas); |
| 306 | |
| 307 | std::function<sk_sp<SkImage>()> imageFactories[] = { |
| 308 | // Create sw raster image. |
| 309 | [bmp] { |
| 310 | return SkImage::MakeFromBitmap(bmp); |
| 311 | }, |
| 312 | // Create encoded image. |
| 313 | [bmp] { |
| 314 | sk_sp<SkData> src( |
| 315 | sk_tool_utils::EncodeImageToData(bmp, SkEncodedImageFormat::kPNG, 100)); |
| 316 | return SkImage::MakeFromEncoded(std::move(src)); |
| 317 | }, |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 318 | // Create YUV encoded image. |
| 319 | [bmp] { |
| 320 | sk_sp<SkData> src( |
| 321 | sk_tool_utils::EncodeImageToData(bmp, SkEncodedImageFormat::kJPEG, 100)); |
| 322 | return SkImage::MakeFromEncoded(std::move(src)); |
| 323 | }, |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 324 | // Create a picture image. |
| 325 | [render_image] { |
| 326 | SkPictureRecorder recorder; |
| 327 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 328 | render_image(canvas); |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 329 | sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 330 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
| 331 | SkISize::Make(kSize, kSize), nullptr, nullptr, |
| 332 | SkImage::BitDepth::kU8, srgbColorSpace); |
| 333 | }, |
| 334 | // Create a texture image |
| 335 | [context, render_image]() -> sk_sp<SkImage> { |
| 336 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, |
| 337 | SkImageInfo::MakeS32(kSize, kSize, |
| 338 | kPremul_SkAlphaType))); |
| 339 | if (!surface) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | render_image(surface->getCanvas()); |
| 343 | return surface->makeImageSnapshot(); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | constexpr SkScalar kPad = 5.f; |
| 348 | canvas->translate(kPad, kPad); |
| 349 | for (auto factory : imageFactories) { |
| 350 | auto image(factory()); |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 351 | if (image) { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 352 | sk_sp<SkImage> texImage(image->makeTextureImage(context, |
| 353 | canvas->imageInfo().colorSpace())); |
| 354 | if (texImage) { |
| 355 | canvas->drawImage(texImage, 0, 0); |
| 356 | } |
| 357 | } |
Robert Phillips | 5bea651 | 2017-02-10 08:04:06 -0500 | [diff] [blame] | 358 | canvas->translate(kSize + kPad, 0); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 359 | } |
| 360 | } |
Mike Reed | ba61af9 | 2017-11-16 10:01:16 -0500 | [diff] [blame] | 361 | |
| 362 | static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pm, SkScalar x, SkScalar y) { |
| 363 | canvas->drawImage(SkImage::MakeRasterCopy(pm), x, y, nullptr); |
| 364 | } |
| 365 | |
| 366 | static void slam_ff(const SkPixmap& pm) { |
| 367 | for (int y = 0; y < pm.height(); ++y) { |
| 368 | for (int x = 0; x < pm.width(); ++x) { |
| 369 | *pm.writable_addr32(x, y) = *pm.addr32(x, y) | SkPackARGB32(0xFF, 0, 0, 0); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | DEF_SIMPLE_GM(scalepixels_unpremul, canvas, 1080, 280) { |
| 375 | SkImageInfo info = SkImageInfo::MakeN32(16, 16, kUnpremul_SkAlphaType); |
| 376 | SkAutoPixmapStorage pm; |
| 377 | pm.alloc(info); |
| 378 | for (int y = 0; y < 16; ++y) { |
| 379 | for (int x = 0; x < 16; ++x) { |
| 380 | *pm.writable_addr32(x, y) = SkPackARGB32NoCheck(0, (y << 4) | y, (x << 4) | x, 0xFF); |
| 381 | } |
| 382 | } |
| 383 | SkAutoPixmapStorage pm2; |
| 384 | pm2.alloc(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType)); |
| 385 | |
| 386 | const SkFilterQuality qualities[] = { |
| 387 | kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality |
| 388 | }; |
| 389 | |
| 390 | for (auto fq : qualities) { |
| 391 | pm.scalePixels(pm2, fq); |
| 392 | slam_ff(pm2); |
| 393 | draw_pixmap(canvas, pm2, 10, 10); |
| 394 | canvas->translate(pm2.width() + 10.0f, 0); |
| 395 | } |
| 396 | } |
Mike Reed | f2c7364 | 2018-05-29 15:41:27 -0400 | [diff] [blame] | 397 | |
| 398 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 399 | |
| 400 | static sk_sp<SkImage> make_lazy_image(SkSurface* surf) { |
| 401 | surf->getCanvas()->drawCircle(100, 100, 100, SkPaint()); |
| 402 | auto data = surf->makeImageSnapshot()->encodeToData(); |
| 403 | return SkImage::MakeFromEncoded(data); |
| 404 | } |
| 405 | |
| 406 | #include "SkWriteBuffer.h" |
| 407 | #include "SkReadBuffer.h" |
| 408 | static sk_sp<SkImage> serial_deserial(SkImage* img) { |
| 409 | SkBinaryWriteBuffer writer; |
| 410 | writer.writeImage(img); |
| 411 | size_t length = writer.bytesWritten(); |
| 412 | auto data = SkData::MakeUninitialized(length); |
| 413 | writer.writeToMemory(data->writable_data()); |
| 414 | |
| 415 | SkReadBuffer reader(data->data(), length); |
| 416 | return reader.readImage(); |
| 417 | } |
| 418 | |
| 419 | DEF_SIMPLE_GM(image_subset, canvas, 440, 220) { |
| 420 | SkImageInfo info = SkImageInfo::MakeN32Premul(200, 200, nullptr); |
| 421 | auto surf = sk_tool_utils::makeSurface(canvas, info, nullptr); |
| 422 | auto img = make_lazy_image(surf.get()); |
| 423 | |
| 424 | canvas->drawImage(img, 10, 10, nullptr); |
| 425 | auto sub = img->makeSubset({100, 100, 200, 200}); |
| 426 | canvas->drawImage(sub, 220, 10); |
| 427 | sub = serial_deserial(sub.get()); |
| 428 | canvas->drawImage(sub, 220+110, 10); |
| 429 | } |