Chromium Code Reviews| Index: gm/drawbitmaprect.cpp |
| diff --git a/gm/drawbitmaprect.cpp b/gm/drawbitmaprect.cpp |
| index fd205f53378b9e36ce0065082eedde8b1d41c54d..8cae9b2c84c2c43b765c4bedc2ec803f5292830e 100644 |
| --- a/gm/drawbitmaprect.cpp |
| +++ b/gm/drawbitmaprect.cpp |
| @@ -1,18 +1,17 @@ |
| - |
| /* |
| * Copyright 2011 Google Inc. |
| * |
| * Use of this source code is governed by a BSD-style license that can be |
| * found in the LICENSE file. |
| */ |
| + |
| #include "gm.h" |
| #include "SkBlurMask.h" |
| #include "SkBlurMaskFilter.h" |
| #include "SkColorPriv.h" |
| #include "SkGradientShader.h" |
| #include "SkShader.h" |
| - |
| -namespace skiagm { |
| +#include "SkImage.h" |
| static SkBitmap make_chessbm(int w, int h) { |
| SkBitmap bm; |
| @@ -28,7 +27,13 @@ static SkBitmap make_chessbm(int w, int h) { |
| return bm; |
| } |
| -static void makebm(SkBitmap* bm, int w, int h) { |
| +static SkImage* image_from_bitmap(const SkBitmap& bm) { |
| + SkBitmap b(bm); |
| + b.lockPixels(); |
| + return SkImage::NewRasterCopy(b.info(), b.getPixels(), b.rowBytes()); |
| +} |
| + |
| +static SkImage* makebm(SkBitmap* bm, int w, int h) { |
| bm->allocN32Pixels(w, h); |
| bm->eraseColor(SK_ColorTRANSPARENT); |
| @@ -70,28 +75,62 @@ static void makebm(SkBitmap* bm, int w, int h) { |
| } |
| // Let backends know we won't change this, so they don't have to deep copy it defensively. |
| bm->setImmutable(); |
| + |
| + return image_from_bitmap(*bm); |
| +} |
| + |
| +static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect* srcR, |
| + const SkRect& dstR) { |
| + canvas->drawBitmapRect(bm, srcR, dstR); |
| } |
| +static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect* srcIR, |
| + const SkRect& dstR) { |
| + SkRect storage, *srcR = NULL; |
| + if (srcIR) { |
| + storage.set(*srcIR); |
| + srcR = &storage; |
| + } |
| + canvas->drawImageRect(image, srcR, dstR); |
| +} |
| + |
| +static void imagescaleproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect* srcIR, |
| + const SkRect& dstR) { |
| + const int newW = SkScalarRoundToInt(dstR.width()); |
| + const int newH = SkScalarRoundToInt(dstR.height()); |
| + SkAutoTUnref<SkImage> newImage(image->newImage(newW, newH, srcIR)); |
| + if (newImage) { |
| + canvas->drawImage(newImage, dstR.x(), dstR.y()); |
| + } |
| +} |
| + |
| +typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect*, const SkRect&); |
| + |
| static const int gSize = 1024; |
| -class DrawBitmapRectGM : public GM { |
| +class DrawBitmapRectGM : public skiagm::GM { |
| public: |
| - DrawBitmapRectGM() { |
| + DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) { |
| + fName.set("drawbitmaprect"); |
| + if (suffix) { |
| + fName.append(suffix); |
| + } |
| } |
| - SkBitmap fLargeBitmap; |
| + DrawRectRectProc* fProc; |
| + SkBitmap fLargeBitmap; |
| + SkAutoTUnref<SkImage> fImage; |
| + SkString fName; |
| protected: |
| - SkString onShortName() { |
| - return SkString("drawbitmaprect"); |
| - } |
| + SkString onShortName() SK_OVERRIDE { return fName; } |
| - SkISize onISize() { return SkISize::Make(gSize, gSize); } |
| + SkISize onISize() SK_OVERRIDE { return SkISize::Make(gSize, gSize); } |
| - virtual void onDraw(SkCanvas* canvas) { |
| + void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| static const int kBmpSize = 2048; |
|
robertphillips
2015/01/22 15:50:02
onOnceBeforeDraw for this ?
reed1
2015/01/22 18:25:51
Done.
|
| if (fLargeBitmap.isNull()) { |
| - makebm(&fLargeBitmap, kBmpSize, kBmpSize); |
| + fImage.reset(makebm(&fLargeBitmap, kBmpSize, kBmpSize)); |
| } |
| SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
| static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2); |
| @@ -126,7 +165,7 @@ protected: |
| SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2, |
| (kBmpSize - h) / 2, |
| w, h); |
| - canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect); |
| + fProc(canvas, fImage, fLargeBitmap, &srcRect, dstRect); |
| SkString label; |
| label.appendf("%d x %d", w, h); |
| @@ -176,11 +215,9 @@ protected: |
| } |
| private: |
| - typedef GM INHERITED; |
| + typedef skiagm::GM INHERITED; |
| }; |
| -////////////////////////////////////////////////////////////////////////////// |
| - |
| -static GM* MyFactory(void*) { return new DrawBitmapRectGM; } |
| -static GMRegistry reg(MyFactory); |
| -} |
| +DEF_GM( return new DrawBitmapRectGM(canvasproc, NULL); ) |
| +DEF_GM( return new DrawBitmapRectGM(imageproc, "_imagerect"); ) |
|
robertphillips
2015/01/22 15:50:02
no '-' ?
reed1
2015/01/22 18:25:51
Done.
|
| +DEF_GM( return new DrawBitmapRectGM(imagescaleproc, "imagescale"); ) |