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" |
| 11 | |
| 12 | static void drawContents(SkSurface* surface, SkColor fillC) { |
| 13 | SkSize size = SkSize::Make(surface->width(), surface->height()); |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame^] | 14 | SkCanvas* canvas = surface->getCanvas(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 15 | |
| 16 | SkScalar stroke = size.fWidth / 10; |
| 17 | SkScalar radius = (size.fWidth - stroke) / 2; |
| 18 | |
| 19 | SkPaint paint; |
| 20 | |
| 21 | paint.setAntiAlias(true); |
| 22 | paint.setColor(fillC); |
| 23 | canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); |
| 24 | |
| 25 | paint.setStyle(SkPaint::kStroke_Style); |
| 26 | paint.setStrokeWidth(stroke); |
| 27 | paint.setColor(SK_ColorBLACK); |
| 28 | canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint); |
| 29 | } |
| 30 | |
| 31 | static void test_surface(SkCanvas* canvas, SkSurface* surf) { |
| 32 | drawContents(surf, SK_ColorRED); |
| 33 | SkImage* imgR = surf->newImageShapshot(); |
| 34 | |
| 35 | drawContents(surf, SK_ColorGREEN); |
| 36 | SkImage* imgG = surf->newImageShapshot(); |
| 37 | |
| 38 | drawContents(surf, SK_ColorBLUE); |
| 39 | |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame^] | 40 | SkPaint paint; |
| 41 | // paint.setFilterBitmap(true); |
| 42 | // paint.setAlpha(0x80); |
| 43 | |
| 44 | imgR->draw(canvas, 0, 0, &paint); |
| 45 | imgG->draw(canvas, 0, 80, &paint); |
| 46 | surf->draw(canvas, 0, 160, &paint); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 47 | |
| 48 | imgG->unref(); |
| 49 | imgR->unref(); |
| 50 | } |
| 51 | |
| 52 | class ImageGM : public skiagm::GM { |
| 53 | void* fBuffer; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 54 | size_t fBufferSize; |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 55 | SkSize fSize; |
| 56 | enum { |
| 57 | W = 64, |
| 58 | H = 64, |
| 59 | RB = W * 4 + 8, |
| 60 | }; |
| 61 | public: |
| 62 | ImageGM() { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 63 | fBufferSize = RB * H; |
| 64 | fBuffer = sk_malloc_throw(fBufferSize); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 65 | fSize.set(SkIntToScalar(W), SkIntToScalar(H)); |
| 66 | } |
| 67 | |
| 68 | virtual ~ImageGM() { |
| 69 | sk_free(fBuffer); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | protected: |
| 74 | virtual SkString onShortName() { |
| 75 | return SkString("image"); |
| 76 | } |
| 77 | |
| 78 | virtual SkISize onISize() { |
| 79 | return SkISize::Make(640, 480); |
| 80 | } |
| 81 | |
| 82 | virtual void onDraw(SkCanvas* canvas) { |
mike@reedtribe.org | d2782ed | 2012-07-31 02:45:15 +0000 | [diff] [blame^] | 83 | canvas->translate(10, 10); |
| 84 | canvas->scale(2, 2); |
| 85 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 86 | // since we draw into this directly, we need to start fresh |
| 87 | sk_bzero(fBuffer, fBufferSize); |
| 88 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 89 | SkImage::Info info; |
| 90 | |
| 91 | info.fWidth = W; |
| 92 | info.fHeight = H; |
| 93 | info.fColorType = SkImage::kPMColor_ColorType; |
| 94 | info.fAlphaType = SkImage::kPremul_AlphaType; |
| 95 | SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, NULL, fBuffer, RB)); |
| 96 | SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, NULL)); |
| 97 | SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight)); |
| 98 | |
| 99 | test_surface(canvas, surf0); |
| 100 | canvas->translate(80, 0); |
| 101 | test_surface(canvas, surf1); |
| 102 | canvas->translate(80, 0); |
| 103 | test_surface(canvas, surf2); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | typedef skiagm::GM INHERITED; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | static skiagm::GM* MyFactory(void*) { return new ImageGM; } |
| 113 | static skiagm::GMRegistry reg(MyFactory); |
| 114 | |