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 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 14 | extern GrContext* GetGr(); |
| 15 | |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 16 | static SkData* fileToData(const char path[]) { |
| 17 | SkFILEStream stream(path); |
| 18 | if (!stream.isValid()) { |
| 19 | return SkData::NewEmpty(); |
| 20 | } |
| 21 | size_t size = stream.getLength(); |
| 22 | void* mem = sk_malloc_throw(size); |
| 23 | stream.read(mem, size); |
| 24 | return SkData::NewFromMalloc(mem, size); |
| 25 | } |
| 26 | |
| 27 | static void drawJpeg(SkCanvas* canvas, const SkISize& size) { |
| 28 | SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); |
| 29 | SkImage* image = SkImage::NewEncodedData(data); |
| 30 | if (image) { |
| 31 | SkAutoCanvasRestore acr(canvas, true); |
| 32 | canvas->scale(size.width() * 1.0f / image->width(), |
| 33 | size.height() * 1.0f / image->height()); |
| 34 | image->draw(canvas,0, 0, NULL); |
| 35 | image->unref(); |
| 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 | |
| 59 | static void test_surface(SkCanvas* canvas, SkSurface* surf) { |
| 60 | drawContents(surf, SK_ColorRED); |
| 61 | SkImage* imgR = surf->newImageShapshot(); |
| 62 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 63 | if (true) { |
| 64 | SkImage* imgR2 = surf->newImageShapshot(); |
| 65 | SkASSERT(imgR == imgR2); |
| 66 | imgR2->unref(); |
| 67 | } |
| 68 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 69 | drawContents(surf, SK_ColorGREEN); |
| 70 | SkImage* imgG = surf->newImageShapshot(); |
| 71 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 72 | // since we've drawn after we snapped imgR, imgG will be a different obj |
| 73 | SkASSERT(imgR != imgG); |
| 74 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 75 | drawContents(surf, SK_ColorBLUE); |
| 76 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 77 | SkPaint paint; |
| 78 | // paint.setFilterBitmap(true); |
| 79 | // paint.setAlpha(0x80); |
| 80 | |
| 81 | imgR->draw(canvas, 0, 0, &paint); |
| 82 | imgG->draw(canvas, 0, 80, &paint); |
| 83 | surf->draw(canvas, 0, 160, &paint); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 84 | |
| 85 | imgG->unref(); |
| 86 | imgR->unref(); |
| 87 | } |
| 88 | |
| 89 | class ImageGM : public skiagm::GM { |
| 90 | void* fBuffer; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 91 | size_t fBufferSize; |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 92 | SkSize fSize; |
| 93 | enum { |
| 94 | W = 64, |
| 95 | H = 64, |
| 96 | RB = W * 4 + 8, |
| 97 | }; |
| 98 | public: |
| 99 | ImageGM() { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 100 | fBufferSize = RB * H; |
| 101 | fBuffer = sk_malloc_throw(fBufferSize); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 102 | fSize.set(SkIntToScalar(W), SkIntToScalar(H)); |
| 103 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 105 | virtual ~ImageGM() { |
| 106 | sk_free(fBuffer); |
| 107 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 108 | |
| 109 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 110 | protected: |
| 111 | virtual SkString onShortName() { |
| 112 | return SkString("image"); |
| 113 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 115 | virtual SkISize onISize() { |
| 116 | return SkISize::Make(640, 480); |
| 117 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 118 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 119 | virtual void onDraw(SkCanvas* canvas) { |
mike@reedtribe.org | d829b5c | 2012-07-31 03:57:11 +0000 | [diff] [blame] | 120 | drawJpeg(canvas, this->getISize()); |
| 121 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame] | 122 | canvas->translate(10, 10); |
| 123 | canvas->scale(2, 2); |
| 124 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 125 | // since we draw into this directly, we need to start fresh |
| 126 | sk_bzero(fBuffer, fBufferSize); |
| 127 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 128 | SkImage::Info info; |
| 129 | |
| 130 | info.fWidth = W; |
| 131 | info.fHeight = H; |
| 132 | info.fColorType = SkImage::kPMColor_ColorType; |
| 133 | info.fAlphaType = SkImage::kPremul_AlphaType; |
| 134 | SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, NULL, fBuffer, RB)); |
| 135 | SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, NULL)); |
| 136 | SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight)); |
| 137 | |
| 138 | test_surface(canvas, surf0); |
| 139 | canvas->translate(80, 0); |
| 140 | test_surface(canvas, surf1); |
| 141 | canvas->translate(80, 0); |
| 142 | test_surface(canvas, surf2); |
| 143 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 145 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
reed@google.com | 9c72827 | 2012-08-28 15:54:54 +0000 | [diff] [blame] | 146 | return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 147 | } |
| 148 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 149 | private: |
| 150 | typedef skiagm::GM INHERITED; |
| 151 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 152 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | static skiagm::GM* MyFactory(void*) { return new ImageGM; } |
| 156 | static skiagm::GMRegistry reg(MyFactory); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | |