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" |
| 16 | |
| 17 | namespace skiagm { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 18 | extern GrContext* GetGr(); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 19 | }; |
| 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 SkData* fileToData(const char path[]) { |
| 23 | SkFILEStream stream(path); |
| 24 | if (!stream.isValid()) { |
| 25 | return SkData::NewEmpty(); |
| 26 | } |
| 27 | size_t size = stream.getLength(); |
| 28 | void* mem = sk_malloc_throw(size); |
| 29 | stream.read(mem, size); |
| 30 | return SkData::NewFromMalloc(mem, size); |
| 31 | } |
| 32 | |
| 33 | static void drawJpeg(SkCanvas* canvas, const SkISize& size) { |
| 34 | SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); |
| 35 | SkImage* image = SkImage::NewEncodedData(data); |
| 36 | if (image) { |
| 37 | SkAutoCanvasRestore acr(canvas, true); |
| 38 | canvas->scale(size.width() * 1.0f / image->width(), |
| 39 | size.height() * 1.0f / image->height()); |
| 40 | image->draw(canvas,0, 0, NULL); |
| 41 | image->unref(); |
| 42 | } |
| 43 | } |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 44 | |
| 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 | |
| 65 | static void test_surface(SkCanvas* canvas, SkSurface* surf) { |
| 66 | drawContents(surf, SK_ColorRED); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 67 | SkImage* imgR = surf->newImageSnapshot(); |
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) { |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 70 | SkImage* imgR2 = surf->newImageSnapshot(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 71 | SkASSERT(imgR == imgR2); |
| 72 | imgR2->unref(); |
| 73 | } |
| 74 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 75 | drawContents(surf, SK_ColorGREEN); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 76 | SkImage* imgG = surf->newImageSnapshot(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 77 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 78 | // since we've drawn after we snapped imgR, imgG will be a different obj |
| 79 | SkASSERT(imgR != imgG); |
| 80 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 81 | drawContents(surf, SK_ColorBLUE); |
| 82 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 83 | SkPaint paint; |
| 84 | // paint.setFilterBitmap(true); |
| 85 | // paint.setAlpha(0x80); |
| 86 | |
| 87 | imgR->draw(canvas, 0, 0, &paint); |
| 88 | imgG->draw(canvas, 0, 80, &paint); |
| 89 | surf->draw(canvas, 0, 160, &paint); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 90 | |
| 91 | imgG->unref(); |
| 92 | imgR->unref(); |
| 93 | } |
| 94 | |
| 95 | class ImageGM : public skiagm::GM { |
| 96 | void* fBuffer; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 97 | size_t fBufferSize; |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 98 | SkSize fSize; |
| 99 | enum { |
| 100 | W = 64, |
| 101 | H = 64, |
| 102 | RB = W * 4 + 8, |
| 103 | }; |
| 104 | public: |
| 105 | ImageGM() { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 106 | fBufferSize = RB * H; |
| 107 | fBuffer = sk_malloc_throw(fBufferSize); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 108 | fSize.set(SkIntToScalar(W), SkIntToScalar(H)); |
| 109 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 110 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 111 | virtual ~ImageGM() { |
| 112 | sk_free(fBuffer); |
| 113 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
| 115 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 116 | protected: |
| 117 | virtual SkString onShortName() { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 118 | return SkString("image-surface"); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 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 SkISize onISize() { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 122 | return SkISize::Make(800, 500); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 123 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 125 | virtual void onDraw(SkCanvas* canvas) { |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 126 | drawJpeg(canvas, this->getISize()); |
| 127 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 128 | canvas->scale(2, 2); |
| 129 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 130 | static const char* kLabel1 = "Original Img"; |
| 131 | static const char* kLabel2 = "Modified Img"; |
| 132 | static const char* kLabel3 = "Cur Surface"; |
| 133 | |
| 134 | static const char* kLabel4 = "Pre-Alloc Img"; |
| 135 | static const char* kLabel5 = "New Alloc Img"; |
| 136 | static const char* kLabel6 = "SkPicture"; |
| 137 | static const char* kLabel7 = "GPU"; |
| 138 | |
| 139 | SkPaint textPaint; |
| 140 | |
| 141 | canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint); |
| 142 | canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint); |
| 143 | canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint); |
| 144 | |
| 145 | canvas->drawText(kLabel4, strlen(kLabel4), 80, 10, textPaint); |
| 146 | canvas->drawText(kLabel5, strlen(kLabel5), 160, 10, textPaint); |
| 147 | canvas->drawText(kLabel6, strlen(kLabel6), 250, 10, textPaint); |
| 148 | canvas->drawText(kLabel7, strlen(kLabel7), 340, 10, textPaint); |
| 149 | |
| 150 | canvas->translate(80, 20); |
| 151 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 152 | // since we draw into this directly, we need to start fresh |
| 153 | sk_bzero(fBuffer, fBufferSize); |
| 154 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 155 | SkImage::Info info; |
| 156 | |
| 157 | info.fWidth = W; |
| 158 | info.fHeight = H; |
| 159 | info.fColorType = SkImage::kPMColor_ColorType; |
| 160 | info.fAlphaType = SkImage::kPremul_AlphaType; |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 161 | SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB)); |
| 162 | SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info)); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 163 | SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight)); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 164 | #if SK_SUPPORT_GPU |
| 165 | GrContext* ctx = skiagm::GetGr(); |
| 166 | |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 167 | SkAutoTUnref<SkSurface> surf3(SkSurface::NewRenderTarget(ctx, info, 0)); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 168 | #endif |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 169 | |
| 170 | test_surface(canvas, surf0); |
| 171 | canvas->translate(80, 0); |
| 172 | test_surface(canvas, surf1); |
| 173 | canvas->translate(80, 0); |
| 174 | test_surface(canvas, surf2); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 175 | #if SK_SUPPORT_GPU |
| 176 | if (NULL != ctx) { |
| 177 | canvas->translate(80, 0); |
| 178 | test_surface(canvas, surf3); |
| 179 | } |
| 180 | #endif |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 181 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 183 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
reed@google.com | 9c72827 | 2012-08-28 15:54:54 +0000 | [diff] [blame] | 184 | return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 185 | } |
| 186 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 187 | private: |
| 188 | typedef skiagm::GM INHERITED; |
| 189 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 190 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 191 | ////////////////////////////////////////////////////////////////////////////// |
| 192 | |
| 193 | static skiagm::GM* MyFactory(void*) { return new ImageGM; } |
| 194 | static skiagm::GMRegistry reg(MyFactory); |