| 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 sk_sp<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 (nullptr == 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; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 paint.setColor(SK_ColorGREEN); | 41 paint.setColor(SK_ColorGREEN); |
| 42 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize), | 42 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize), |
| 43 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); | 43 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 44 canvas->drawRect(r, paint); | 44 canvas->drawRect(r, paint); |
| 45 | 45 |
| 46 paint.setColor(SK_ColorTRANSPARENT); | 46 paint.setColor(SK_ColorTRANSPARENT); |
| 47 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize), | 47 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize), |
| 48 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); | 48 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 49 canvas->drawRect(r, paint); | 49 canvas->drawRect(r, paint); |
| 50 | 50 |
| 51 return surface->newImageSnapshot(); | 51 return surface->makeImageSnapshot(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 // This GM tests the drawAtlas API with colors, different xfer modes | 54 // This GM tests the drawAtlas API with colors, different xfer modes |
| 55 // and transparency in the atlas image | 55 // and transparency in the atlas image |
| 56 class DrawAtlasColorsGM : public skiagm::GM { | 56 class DrawAtlasColorsGM : public skiagm::GM { |
| 57 public: | 57 public: |
| 58 DrawAtlasColorsGM() { | 58 DrawAtlasColorsGM() { |
| 59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 protected: | 62 protected: |
| 63 SkString onShortName() override { | 63 SkString onShortName() override { |
| 64 return SkString("draw-atlas-colors"); | 64 return SkString("draw-atlas-colors"); |
| 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 (nullptr == fAtlas) { | 75 if (nullptr == fAtlas) { |
| 76 fAtlas.reset(make_atlas(canvas, kAtlasSize)); | 76 fAtlas = 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" }, |
| 86 { SkXfermode::kSrcOver_Mode, "SrcOver" }, | 86 { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.get(), xforms, rects, quadColors, numColors
, |
| 155 gModes[i].fMode, nullptr, nullptr); | 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.get(), xforms, rects, quadColors, numColors
, |
| 159 gModes[i].fMode, nullptr, &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 sk_sp<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 |