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