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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkSurface.h" |
| 10 | #include "SkCanvas.h" |
piotaixr | 0d276f7 | 2014-09-18 11:55:14 -0700 | [diff] [blame] | 11 | #include "SkDecodingImageGenerator.h" |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 12 | #include "SkStream.h" |
| 13 | #include "SkData.h" |
| 14 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 15 | #if SK_SUPPORT_GPU |
| 16 | #include "GrContext.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 17 | #endif |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 18 | |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 19 | static void drawJpeg(SkCanvas* canvas, const SkISize& size) { |
scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 20 | // TODO: Make this draw a file that is checked in, so it can |
| 21 | // be exercised on machines other than mike's. Will require a |
| 22 | // rebaseline. |
reed | 48925e3 | 2014-09-18 13:57:05 -0700 | [diff] [blame] | 23 | SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg")); |
| 24 | if (NULL == data.get()) { |
| 25 | return; |
| 26 | } |
piotaixr | 0d276f7 | 2014-09-18 11:55:14 -0700 | [diff] [blame] | 27 | SkImage* image = SkImage::NewFromGenerator( |
| 28 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options())); |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 29 | if (image) { |
| 30 | SkAutoCanvasRestore acr(canvas, true); |
| 31 | canvas->scale(size.width() * 1.0f / image->width(), |
| 32 | size.height() * 1.0f / image->height()); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 33 | canvas->drawImage(image, 0, 0, NULL); |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 34 | image->unref(); |
| 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); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 60 | SkImage* imgR = surf->newImageSnapshot(); |
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) { |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 63 | SkImage* imgR2 = surf->newImageSnapshot(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 64 | SkASSERT(imgR == imgR2); |
| 65 | imgR2->unref(); |
| 66 | } |
| 67 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 68 | drawContents(surf, SK_ColorGREEN); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 69 | SkImage* imgG = surf->newImageSnapshot(); |
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 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 80 | canvas->drawImage(imgR, 0, 0, usePaint ? &paint : NULL); |
| 81 | canvas->drawImage(imgG, 0, 80, usePaint ? &paint : NULL); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 82 | surf->draw(canvas, 0, 160, usePaint ? &paint : NULL); |
| 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 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 96 | canvas->drawImageRect(imgR, &src1, dst1, usePaint ? &paint : NULL); |
| 97 | canvas->drawImageRect(imgG, &src2, dst2, usePaint ? &paint : NULL); |
| 98 | canvas->drawImageRect(imgR, &src3, dst3, usePaint ? &paint : NULL); |
| 99 | canvas->drawImageRect(imgG, NULL, dst4, usePaint ? &paint : NULL); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 100 | |
| 101 | imgG->unref(); |
| 102 | imgR->unref(); |
| 103 | } |
| 104 | |
| 105 | class ImageGM : public skiagm::GM { |
| 106 | void* fBuffer; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 107 | size_t fBufferSize; |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 108 | SkSize fSize; |
| 109 | enum { |
| 110 | W = 64, |
| 111 | H = 64, |
| 112 | RB = W * 4 + 8, |
| 113 | }; |
| 114 | public: |
| 115 | ImageGM() { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 116 | fBufferSize = RB * H; |
| 117 | fBuffer = sk_malloc_throw(fBufferSize); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 118 | fSize.set(SkIntToScalar(W), SkIntToScalar(H)); |
| 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 | virtual ~ImageGM() { |
| 122 | sk_free(fBuffer); |
| 123 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
| 125 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 126 | protected: |
| 127 | virtual SkString onShortName() { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 128 | return SkString("image-surface"); |
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 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 131 | virtual SkISize onISize() { |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 132 | return SkISize::Make(960, 1200); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 133 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 135 | virtual void onDraw(SkCanvas* canvas) { |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 136 | drawJpeg(canvas, this->getISize()); |
| 137 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 138 | canvas->scale(2, 2); |
| 139 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 140 | static const char* kLabel1 = "Original Img"; |
| 141 | static const char* kLabel2 = "Modified Img"; |
| 142 | static const char* kLabel3 = "Cur Surface"; |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 143 | static const char* kLabel4 = "Full Crop"; |
| 144 | static const char* kLabel5 = "Over-crop"; |
| 145 | static const char* kLabel6 = "Upper-left"; |
| 146 | static const char* kLabel7 = "No Crop"; |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 147 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 148 | static const char* kLabel8 = "Pre-Alloc Img"; |
| 149 | static const char* kLabel9 = "New Alloc Img"; |
commit-bot@chromium.org | cae54f1 | 2014-04-11 18:34:35 +0000 | [diff] [blame] | 150 | static const char* kLabel10 = "Null Paint"; |
| 151 | static const char* kLabel11 = "GPU"; |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 152 | |
| 153 | SkPaint textPaint; |
commit-bot@chromium.org | cae54f1 | 2014-04-11 18:34:35 +0000 | [diff] [blame] | 154 | textPaint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 155 | sk_tool_utils::set_portable_typeface(&textPaint); |
commit-bot@chromium.org | cae54f1 | 2014-04-11 18:34:35 +0000 | [diff] [blame] | 156 | textPaint.setTextSize(8); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 157 | |
| 158 | canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint); |
| 159 | canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint); |
| 160 | canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 161 | canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint); |
| 162 | canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint); |
| 163 | canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint); |
| 164 | canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 165 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 166 | canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint); |
| 167 | canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint); |
| 168 | canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint); |
| 169 | canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 170 | |
| 171 | canvas->translate(80, 20); |
| 172 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 173 | // since we draw into this directly, we need to start fresh |
| 174 | sk_bzero(fBuffer, fBufferSize); |
| 175 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 176 | SkImageInfo info = SkImageInfo::MakeN32Premul(W, H); |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 177 | SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB)); |
| 178 | SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info)); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 179 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 180 | GrContext* ctx = canvas->getGrContext(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 181 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 182 | SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info)); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 183 | #endif |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 184 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 185 | test_surface(canvas, surf0, true); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 186 | canvas->translate(80, 0); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 187 | test_surface(canvas, surf1, true); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 188 | #if SK_SUPPORT_GPU |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 189 | if (ctx) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 190 | canvas->translate(80, 0); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 191 | test_surface(canvas, surf4, true); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 192 | } |
| 193 | #endif |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 194 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 195 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 196 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
reed@google.com | 9c72827 | 2012-08-28 15:54:54 +0000 | [diff] [blame] | 197 | return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 198 | } |
| 199 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 200 | private: |
| 201 | typedef skiagm::GM INHERITED; |
| 202 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 203 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 204 | ////////////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | static skiagm::GM* MyFactory(void*) { return new ImageGM; } |
| 207 | static skiagm::GMRegistry reg(MyFactory); |