Index: gm/colormatrix.cpp |
diff --git a/gm/colormatrix.cpp b/gm/colormatrix.cpp |
index 8dcdf15e50167e2a1d85fb6a1f52b94f8429a5ed..a80f8cb5814ac92cb8b86da34ff66f3270587b02 100644 |
--- a/gm/colormatrix.cpp |
+++ b/gm/colormatrix.cpp |
@@ -8,60 +8,40 @@ |
#include "gm.h" |
#include "SkColorMatrixFilter.h" |
#include "SkGradientShader.h" |
+#include "SkImage.h" |
#define WIDTH 500 |
#define HEIGHT 500 |
-class SkDoOnce { |
-public: |
- SkDoOnce() : fOnce(false) {}; |
- |
- bool once() const { |
- if (fOnce) { |
- return false; |
- } |
- fOnce = true; |
- return true; |
- } |
- |
-private: |
- mutable bool fOnce; |
-}; |
- |
-static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) { |
+static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) { |
paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref(); |
} |
-static void setArray(SkPaint* paint, const SkScalar array[]) { |
+static void set_array(SkPaint* paint, const SkScalar array[]) { |
paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref(); |
} |
-namespace skiagm { |
- |
-class ColorMatrixGM : public GM { |
- SkDoOnce fOnce; |
- void init() { |
- if (fOnce.once()) { |
- fSolidBitmap = this->createSolidBitmap(64, 64); |
- fTransparentBitmap = this->createTransparentBitmap(64, 64); |
- } |
- } |
- |
+class ColorMatrixGM : public skiagm::GM { |
public: |
ColorMatrixGM() { |
this->setBGColor(sk_tool_utils::color_to_565(0xFF808080)); |
} |
protected: |
- virtual SkString onShortName() { |
+ SkString onShortName() override { |
return SkString("colormatrix"); |
} |
- virtual SkISize onISize() { |
+ SkISize onISize() override { |
return SkISize::Make(WIDTH, HEIGHT); |
} |
+ |
+ void onOnceBeforeDraw() override { |
+ fSolidImg.reset(CreateSolidBitmap(64, 64)); |
+ fTransparentImg.reset(CreateTransparentBitmap(64, 64)); |
+ } |
- SkBitmap createSolidBitmap(int width, int height) { |
+ static SkImage* CreateSolidBitmap(int width, int height) { |
SkBitmap bm; |
bm.allocN32Pixels(width, height); |
SkCanvas canvas(bm); |
@@ -74,11 +54,11 @@ protected: |
SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); |
} |
} |
- return bm; |
+ return SkImage::NewFromBitmap(bm); |
} |
// creates a bitmap with shades of transparent gray. |
- SkBitmap createTransparentBitmap(int width, int height) { |
+ static SkImage* CreateTransparentBitmap(int width, int height) { |
SkBitmap bm; |
bm.allocN32Pixels(width, height); |
SkCanvas canvas(bm); |
@@ -90,58 +70,56 @@ protected: |
paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
SkShader::kClamp_TileMode))->unref(); |
canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint); |
- return bm; |
+ return SkImage::NewFromBitmap(bm); |
} |
- virtual void onDraw(SkCanvas* canvas) { |
- this->init(); |
- |
+ void onDraw(SkCanvas* canvas) override { |
SkPaint paint; |
SkColorMatrix matrix; |
paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
- const SkBitmap bmps[] = { fSolidBitmap, fTransparentBitmap }; |
+ const SkImage* bmps[] = { fSolidImg, fTransparentImg }; |
for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) { |
matrix.setIdentity(); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 0, 0, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 0, 0, &paint); |
matrix.setRotate(SkColorMatrix::kR_Axis, 90); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 80, 0, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 80, 0, &paint); |
matrix.setRotate(SkColorMatrix::kG_Axis, 90); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 160, 0, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 160, 0, &paint); |
matrix.setRotate(SkColorMatrix::kB_Axis, 90); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 240, 0, &paint); |
- |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 240, 0, &paint); |
+ /////////////////////////////////////////////// |
matrix.setSaturation(0.0f); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 0, 80, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 0, 80, &paint); |
matrix.setSaturation(0.5f); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 80, 80, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 80, 80, &paint); |
matrix.setSaturation(1.0f); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 160, 80, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 160, 80, &paint); |
matrix.setSaturation(2.0f); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 240, 80, &paint); |
- |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 240, 80, &paint); |
+ /////////////////////////////////////////////// |
matrix.setRGB2YUV(); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 0, 160, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 0, 160, &paint); |
matrix.setYUV2RGB(); |
- setColorMatrix(&paint, matrix); |
- canvas->drawBitmap(bmps[i], 80, 160, &paint); |
+ set_color_matrix(&paint, matrix); |
+ canvas->drawImage(bmps[i], 80, 160, &paint); |
SkScalar s1 = SK_Scalar1; |
SkScalar s255 = SkIntToScalar(255); |
@@ -153,22 +131,18 @@ protected: |
s1, 0, 0, 0, 0, |
}; |
- setArray(&paint, data); |
- canvas->drawBitmap(bmps[i], 160, 160, &paint); |
- |
+ set_array(&paint, data); |
+ canvas->drawImage(bmps[i], 160, 160, &paint); |
+ /////////////////////////////////////////////// |
canvas->translate(0, 240); |
} |
} |
private: |
- SkBitmap fSolidBitmap; |
- SkBitmap fTransparentBitmap; |
- typedef GM INHERITED; |
-}; |
- |
-////////////////////////////////////////////////////////////////////////////// |
+ SkAutoTUnref<SkImage> fSolidImg; |
+ SkAutoTUnref<SkImage> fTransparentImg; |
-static GM* MyFactory(void*) { return new ColorMatrixGM; } |
-static GMRegistry reg(MyFactory); |
+ typedef skiagm::GM INHERITED; |
+}; |
+DEF_GM( return new ColorMatrixGM; ) |
-} |