Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkColorMatrixFilter.h" | 9 #include "SkColorMatrixFilter.h" |
| 10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
| 11 #include "SkImage.h" | |
| 11 | 12 |
| 12 #define WIDTH 500 | 13 #define WIDTH 500 |
| 13 #define HEIGHT 500 | 14 #define HEIGHT 500 |
| 14 | 15 |
| 15 class SkDoOnce { | 16 class SkDoOnce { |
| 16 public: | 17 public: |
| 17 SkDoOnce() : fOnce(false) {}; | 18 SkDoOnce() : fOnce(false) {}; |
| 18 | 19 |
| 19 bool once() const { | 20 bool once() const { |
| 20 if (fOnce) { | 21 if (fOnce) { |
| 21 return false; | 22 return false; |
| 22 } | 23 } |
| 23 fOnce = true; | 24 fOnce = true; |
| 24 return true; | 25 return true; |
| 25 } | 26 } |
| 26 | 27 |
| 27 private: | 28 private: |
| 28 mutable bool fOnce; | 29 mutable bool fOnce; |
| 29 }; | 30 }; |
| 30 | 31 |
|
robertphillips
2015/07/06 20:43:47
set_color_matrix ?
reed2
2015/07/07 01:18:02
Done.
| |
| 31 static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) { | 32 static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) { |
| 32 paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref(); | 33 paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref(); |
| 33 } | 34 } |
| 34 | 35 |
|
robertphillips
2015/07/06 20:43:47
set_array ?
reed2
2015/07/07 01:18:02
Done.
| |
| 35 static void setArray(SkPaint* paint, const SkScalar array[]) { | 36 static void setArray(SkPaint* paint, const SkScalar array[]) { |
| 36 paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref(); | 37 paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 namespace skiagm { | 40 class ColorMatrixGM : public skiagm::GM { |
| 40 | |
| 41 class ColorMatrixGM : public GM { | |
| 42 SkDoOnce fOnce; | 41 SkDoOnce fOnce; |
|
robertphillips
2015/07/06 20:43:47
Can this not occur in onOnceBeforeDraw ?
reed2
2015/07/07 01:18:02
Done.
| |
| 43 void init() { | 42 void init() { |
| 44 if (fOnce.once()) { | 43 if (fOnce.once()) { |
| 45 fSolidBitmap = this->createSolidBitmap(64, 64); | 44 fSolidImg.reset(this->createSolidBitmap(64, 64)); |
| 46 fTransparentBitmap = this->createTransparentBitmap(64, 64); | 45 fTransparentImg.reset(this->createTransparentBitmap(64, 64)); |
| 47 } | 46 } |
| 48 } | 47 } |
| 49 | 48 |
| 50 public: | 49 public: |
| 51 ColorMatrixGM() { | 50 ColorMatrixGM() { |
| 52 this->setBGColor(sk_tool_utils::color_to_565(0xFF808080)); | 51 this->setBGColor(sk_tool_utils::color_to_565(0xFF808080)); |
| 53 } | 52 } |
| 54 | 53 |
| 55 protected: | 54 protected: |
|
robertphillips
2015/07/06 20:43:47
override(s) ?
reed2
2015/07/07 01:18:02
Done.
| |
| 56 virtual SkString onShortName() { | 55 virtual SkString onShortName() { |
| 57 return SkString("colormatrix"); | 56 return SkString("colormatrix"); |
| 58 } | 57 } |
| 59 | 58 |
| 60 virtual SkISize onISize() { | 59 virtual SkISize onISize() { |
| 61 return SkISize::Make(WIDTH, HEIGHT); | 60 return SkISize::Make(WIDTH, HEIGHT); |
| 62 } | 61 } |
| 63 | 62 |
|
robertphillips
2015/07/06 20:43:47
Can createSolidBitmap & createTransparentBitmap be
reed2
2015/07/07 01:18:02
Done.
| |
| 64 SkBitmap createSolidBitmap(int width, int height) { | 63 SkImage* createSolidBitmap(int width, int height) { |
| 65 SkBitmap bm; | 64 SkBitmap bm; |
| 66 bm.allocN32Pixels(width, height); | 65 bm.allocN32Pixels(width, height); |
| 67 SkCanvas canvas(bm); | 66 SkCanvas canvas(bm); |
| 68 canvas.clear(0x0); | 67 canvas.clear(0x0); |
| 69 for (int y = 0; y < height; ++y) { | 68 for (int y = 0; y < height; ++y) { |
| 70 for (int x = 0; x < width; ++x) { | 69 for (int x = 0; x < width; ++x) { |
| 71 SkPaint paint; | 70 SkPaint paint; |
| 72 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / he ight, 0)); | 71 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / he ight, 0)); |
| 73 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x), | 72 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x), |
| 74 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); | 73 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); |
| 75 } | 74 } |
| 76 } | 75 } |
| 77 return bm; | 76 return SkImage::NewFromBitmap(bm); |
| 78 } | 77 } |
| 79 | 78 |
| 80 // creates a bitmap with shades of transparent gray. | 79 // creates a bitmap with shades of transparent gray. |
| 81 SkBitmap createTransparentBitmap(int width, int height) { | 80 SkImage* createTransparentBitmap(int width, int height) { |
| 82 SkBitmap bm; | 81 SkBitmap bm; |
| 83 bm.allocN32Pixels(width, height); | 82 bm.allocN32Pixels(width, height); |
| 84 SkCanvas canvas(bm); | 83 SkCanvas canvas(bm); |
| 85 canvas.clear(0x0); | 84 canvas.clear(0x0); |
| 86 | 85 |
| 87 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}}; | 86 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}}; |
| 88 SkColor colors[] = {0x00000000, 0xFFFFFFFF}; | 87 SkColor colors[] = {0x00000000, 0xFFFFFFFF}; |
| 89 SkPaint paint; | 88 SkPaint paint; |
| 90 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, | 89 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 91 SkShader::kClamp_TileMode ))->unref(); | 90 SkShader::kClamp_TileMode ))->unref(); |
| 92 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heigh t)), paint); | 91 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heigh t)), paint); |
| 93 return bm; | 92 return SkImage::NewFromBitmap(bm); |
| 94 } | 93 } |
| 95 | 94 |
| 96 virtual void onDraw(SkCanvas* canvas) { | 95 virtual void onDraw(SkCanvas* canvas) { |
| 97 this->init(); | 96 this->init(); |
| 98 | 97 |
| 99 SkPaint paint; | 98 SkPaint paint; |
| 100 SkColorMatrix matrix; | 99 SkColorMatrix matrix; |
| 101 | 100 |
| 102 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 101 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 103 const SkBitmap bmps[] = { fSolidBitmap, fTransparentBitmap }; | 102 const SkImage* bmps[] = { fSolidImg, fTransparentImg }; |
| 104 | 103 |
| 105 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) { | 104 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) { |
| 106 matrix.setIdentity(); | 105 matrix.setIdentity(); |
| 107 setColorMatrix(&paint, matrix); | 106 setColorMatrix(&paint, matrix); |
| 108 canvas->drawBitmap(bmps[i], 0, 0, &paint); | 107 canvas->drawImage(bmps[i], 0, 0, &paint); |
| 109 | 108 |
| 110 matrix.setRotate(SkColorMatrix::kR_Axis, 90); | 109 matrix.setRotate(SkColorMatrix::kR_Axis, 90); |
| 111 setColorMatrix(&paint, matrix); | 110 setColorMatrix(&paint, matrix); |
| 112 canvas->drawBitmap(bmps[i], 80, 0, &paint); | 111 canvas->drawImage(bmps[i], 80, 0, &paint); |
| 113 | 112 |
| 114 matrix.setRotate(SkColorMatrix::kG_Axis, 90); | 113 matrix.setRotate(SkColorMatrix::kG_Axis, 90); |
| 115 setColorMatrix(&paint, matrix); | 114 setColorMatrix(&paint, matrix); |
| 116 canvas->drawBitmap(bmps[i], 160, 0, &paint); | 115 canvas->drawImage(bmps[i], 160, 0, &paint); |
| 117 | 116 |
| 118 matrix.setRotate(SkColorMatrix::kB_Axis, 90); | 117 matrix.setRotate(SkColorMatrix::kB_Axis, 90); |
| 119 setColorMatrix(&paint, matrix); | 118 setColorMatrix(&paint, matrix); |
| 120 canvas->drawBitmap(bmps[i], 240, 0, &paint); | 119 canvas->drawImage(bmps[i], 240, 0, &paint); |
| 121 | 120 /////////////////////////////////////////////// |
| 122 matrix.setSaturation(0.0f); | 121 matrix.setSaturation(0.0f); |
| 123 setColorMatrix(&paint, matrix); | 122 setColorMatrix(&paint, matrix); |
| 124 canvas->drawBitmap(bmps[i], 0, 80, &paint); | 123 canvas->drawImage(bmps[i], 0, 80, &paint); |
| 125 | 124 |
| 126 matrix.setSaturation(0.5f); | 125 matrix.setSaturation(0.5f); |
| 127 setColorMatrix(&paint, matrix); | 126 setColorMatrix(&paint, matrix); |
| 128 canvas->drawBitmap(bmps[i], 80, 80, &paint); | 127 canvas->drawImage(bmps[i], 80, 80, &paint); |
| 129 | 128 |
| 130 matrix.setSaturation(1.0f); | 129 matrix.setSaturation(1.0f); |
| 131 setColorMatrix(&paint, matrix); | 130 setColorMatrix(&paint, matrix); |
| 132 canvas->drawBitmap(bmps[i], 160, 80, &paint); | 131 canvas->drawImage(bmps[i], 160, 80, &paint); |
| 133 | 132 |
| 134 matrix.setSaturation(2.0f); | 133 matrix.setSaturation(2.0f); |
| 135 setColorMatrix(&paint, matrix); | 134 setColorMatrix(&paint, matrix); |
| 136 canvas->drawBitmap(bmps[i], 240, 80, &paint); | 135 canvas->drawImage(bmps[i], 240, 80, &paint); |
| 137 | 136 /////////////////////////////////////////////// |
| 138 matrix.setRGB2YUV(); | 137 matrix.setRGB2YUV(); |
| 139 setColorMatrix(&paint, matrix); | 138 setColorMatrix(&paint, matrix); |
| 140 canvas->drawBitmap(bmps[i], 0, 160, &paint); | 139 canvas->drawImage(bmps[i], 0, 160, &paint); |
| 141 | 140 |
| 142 matrix.setYUV2RGB(); | 141 matrix.setYUV2RGB(); |
| 143 setColorMatrix(&paint, matrix); | 142 setColorMatrix(&paint, matrix); |
| 144 canvas->drawBitmap(bmps[i], 80, 160, &paint); | 143 canvas->drawImage(bmps[i], 80, 160, &paint); |
| 145 | 144 |
| 146 SkScalar s1 = SK_Scalar1; | 145 SkScalar s1 = SK_Scalar1; |
| 147 SkScalar s255 = SkIntToScalar(255); | 146 SkScalar s255 = SkIntToScalar(255); |
| 148 // Move red into alpha, set color to white | 147 // Move red into alpha, set color to white |
| 149 SkScalar data[20] = { | 148 SkScalar data[20] = { |
| 150 0, 0, 0, 0, s255, | 149 0, 0, 0, 0, s255, |
| 151 0, 0, 0, 0, s255, | 150 0, 0, 0, 0, s255, |
| 152 0, 0, 0, 0, s255, | 151 0, 0, 0, 0, s255, |
| 153 s1, 0, 0, 0, 0, | 152 s1, 0, 0, 0, 0, |
| 154 }; | 153 }; |
| 155 | 154 |
| 156 setArray(&paint, data); | 155 setArray(&paint, data); |
| 157 canvas->drawBitmap(bmps[i], 160, 160, &paint); | 156 canvas->drawImage(bmps[i], 160, 160, &paint); |
| 158 | 157 /////////////////////////////////////////////// |
| 159 canvas->translate(0, 240); | 158 canvas->translate(0, 240); |
| 160 } | 159 } |
| 161 } | 160 } |
| 162 | 161 |
| 163 private: | 162 private: |
| 164 SkBitmap fSolidBitmap; | 163 SkAutoTUnref<SkImage> fSolidImg; |
| 165 SkBitmap fTransparentBitmap; | 164 SkAutoTUnref<SkImage> fTransparentImg; |
| 166 typedef GM INHERITED; | 165 |
| 166 typedef skiagm::GM INHERITED; | |
| 167 }; | 167 }; |
| 168 DEF_GM( return new ColorMatrixGM; ) | |
| 168 | 169 |
| 169 ////////////////////////////////////////////////////////////////////////////// | |
| 170 | |
| 171 static GM* MyFactory(void*) { return new ColorMatrixGM; } | |
| 172 static GMRegistry reg(MyFactory); | |
| 173 | |
| 174 } | |
| OLD | NEW |