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