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