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