| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkRSXform.h" | 10 #include "SkRSXform.h" |
| 11 #include "SkSurface.h" | 11 #include "SkSurface.h" |
| 12 | 12 |
| 13 // Create a square atlas of: | 13 // Create a square atlas of: |
| 14 // opaque white | opaque red | 14 // opaque white | opaque red |
| 15 // ------------------------------------ | 15 // ------------------------------------ |
| 16 // opaque green | transparent black | 16 // opaque green | transparent black |
| 17 // | 17 // |
| 18 static SkImage* make_atlas(SkCanvas* caller, int atlasSize) { | 18 static SkImage* make_atlas(SkCanvas* caller, int atlasSize) { |
| 19 const int kBlockSize = atlasSize/2; | 19 const int kBlockSize = atlasSize/2; |
| 20 | 20 |
| 21 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); | 21 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); |
| 22 SkAutoTUnref<SkSurface> surface(caller->newSurface(info)); | 22 SkAutoTUnref<SkSurface> surface(caller->newSurface(info)); |
| 23 if (NULL == surface) { | 23 if (nullptr == surface) { |
| 24 surface.reset(SkSurface::NewRaster(info)); | 24 surface.reset(SkSurface::NewRaster(info)); |
| 25 } | 25 } |
| 26 SkCanvas* canvas = surface->getCanvas(); | 26 SkCanvas* canvas = surface->getCanvas(); |
| 27 | 27 |
| 28 SkPaint paint; | 28 SkPaint paint; |
| 29 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode)); | 29 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode)); |
| 30 | 30 |
| 31 paint.setColor(SK_ColorWHITE); | 31 paint.setColor(SK_ColorWHITE); |
| 32 SkRect r = SkRect::MakeXYWH(0, 0, | 32 SkRect r = SkRect::MakeXYWH(0, 0, |
| 33 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockS
ize)); | 33 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockS
ize)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 65 } |
| 66 | 66 |
| 67 SkISize onISize() override { | 67 SkISize onISize() override { |
| 68 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad, | 68 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad, |
| 69 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + k
Pad); | 69 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + k
Pad); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void onDraw(SkCanvas* canvas) override { | 72 void onDraw(SkCanvas* canvas) override { |
| 73 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToS
calar(kAtlasSize)); | 73 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToS
calar(kAtlasSize)); |
| 74 | 74 |
| 75 if (NULL == fAtlas) { | 75 if (nullptr == fAtlas) { |
| 76 fAtlas.reset(make_atlas(canvas, kAtlasSize)); | 76 fAtlas.reset(make_atlas(canvas, kAtlasSize)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 const struct { | 79 const struct { |
| 80 SkXfermode::Mode fMode; | 80 SkXfermode::Mode fMode; |
| 81 const char* fLabel; | 81 const char* fLabel; |
| 82 } gModes[] = { | 82 } gModes[] = { |
| 83 { SkXfermode::kClear_Mode, "Clear" }, | 83 { SkXfermode::kClear_Mode, "Clear" }, |
| 84 { SkXfermode::kSrc_Mode, "Src" }, | 84 { SkXfermode::kSrc_Mode, "Src" }, |
| 85 { SkXfermode::kDst_Mode, "Dst" }, | 85 { SkXfermode::kDst_Mode, "Dst" }, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 for (int i = 0; i < numColors; ++i) { | 132 for (int i = 0; i < numColors; ++i) { |
| 133 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPa
d)); | 133 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPa
d)); |
| 134 rects[i] = target; | 134 rects[i] = target; |
| 135 quadColors[i] = gColors[i]; | 135 quadColors[i] = gColors[i]; |
| 136 } | 136 } |
| 137 | 137 |
| 138 SkPaint textP; | 138 SkPaint textP; |
| 139 textP.setTextSize(SkIntToScalar(kTextPad)); | 139 textP.setTextSize(SkIntToScalar(kTextPad)); |
| 140 textP.setAntiAlias(true); | 140 textP.setAntiAlias(true); |
| 141 sk_tool_utils::set_portable_typeface(&textP, NULL); | 141 sk_tool_utils::set_portable_typeface(&textP, nullptr); |
| 142 | 142 |
| 143 for (int i = 0; i < numModes; ++i) { | 143 for (int i = 0; i < numModes; ++i) { |
| 144 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel), | 144 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel), |
| 145 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPa
d), | 145 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPa
d), |
| 146 textP); | 146 textP); |
| 147 } | 147 } |
| 148 | 148 |
| 149 for (int i = 0; i < numModes; ++i) { | 149 for (int i = 0; i < numModes; ++i) { |
| 150 canvas->save(); | 150 canvas->save(); |
| 151 canvas->translate(SkIntToScalar(i*(target.height()+kPad)), | 151 canvas->translate(SkIntToScalar(i*(target.height()+kPad)), |
| 152 SkIntToScalar(kTextPad+kPad)); | 152 SkIntToScalar(kTextPad+kPad)); |
| 153 // w/o a paint | 153 // w/o a paint |
| 154 canvas->drawAtlas(fAtlas, xforms, rects, quadColors, numColors, | 154 canvas->drawAtlas(fAtlas, xforms, rects, quadColors, numColors, |
| 155 gModes[i].fMode, NULL, NULL); | 155 gModes[i].fMode, nullptr, nullptr); |
| 156 canvas->translate(0.0f, numColors*(target.height()+kPad)); | 156 canvas->translate(0.0f, numColors*(target.height()+kPad)); |
| 157 // w a paint | 157 // w a paint |
| 158 canvas->drawAtlas(fAtlas, xforms, rects, quadColors, numColors, | 158 canvas->drawAtlas(fAtlas, xforms, rects, quadColors, numColors, |
| 159 gModes[i].fMode, NULL, &paint); | 159 gModes[i].fMode, nullptr, &paint); |
| 160 canvas->restore(); | 160 canvas->restore(); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 private: | 164 private: |
| 165 static const int kNumXferModes = 29; | 165 static const int kNumXferModes = 29; |
| 166 static const int kNumColors = 4; | 166 static const int kNumColors = 4; |
| 167 static const int kAtlasSize = 30; | 167 static const int kAtlasSize = 30; |
| 168 static const int kPad = 2; | 168 static const int kPad = 2; |
| 169 static const int kTextPad = 8; | 169 static const int kTextPad = 8; |
| 170 | 170 |
| 171 | 171 |
| 172 SkAutoTUnref<SkImage> fAtlas; | 172 SkAutoTUnref<SkImage> fAtlas; |
| 173 | 173 |
| 174 typedef GM INHERITED; | 174 typedef GM INHERITED; |
| 175 }; | 175 }; |
| 176 DEF_GM( return new DrawAtlasColorsGM; ) | 176 DEF_GM( return new DrawAtlasColorsGM; ) |
| 177 | 177 |
| OLD | NEW |