image_shader.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkColorSpace.h"
  11. #include "include/core/SkData.h"
  12. #include "include/core/SkEncodedImageFormat.h"
  13. #include "include/core/SkImage.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkMatrix.h"
  16. #include "include/core/SkPaint.h"
  17. #include "include/core/SkPicture.h"
  18. #include "include/core/SkPictureRecorder.h"
  19. #include "include/core/SkRect.h"
  20. #include "include/core/SkRefCnt.h"
  21. #include "include/core/SkShader.h"
  22. #include "include/core/SkSize.h"
  23. #include "include/core/SkString.h"
  24. #include "include/core/SkSurface.h"
  25. #include "include/core/SkTileMode.h"
  26. #include "include/core/SkTypes.h"
  27. #include <utility>
  28. class GrContext;
  29. static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
  30. SkPaint paint;
  31. paint.setAntiAlias(true);
  32. paint.setColor(SK_ColorRED);
  33. paint.setStyle(SkPaint::kStroke_Style);
  34. paint.setStrokeWidth(10);
  35. canvas->drawRect(bounds, paint);
  36. paint.setStyle(SkPaint::kFill_Style);
  37. paint.setColor(SK_ColorBLUE);
  38. canvas->drawOval(bounds, paint);
  39. }
  40. typedef sk_sp<SkImage> (*ImageMakerProc)(GrContext*, SkPicture*, const SkImageInfo&);
  41. static sk_sp<SkImage> make_raster(GrContext*, SkPicture* pic, const SkImageInfo& info) {
  42. auto surface(SkSurface::MakeRaster(info));
  43. surface->getCanvas()->clear(0);
  44. surface->getCanvas()->drawPicture(pic);
  45. return surface->makeImageSnapshot();
  46. }
  47. static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
  48. if (!ctx) {
  49. return nullptr;
  50. }
  51. auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
  52. if (!surface) {
  53. return nullptr;
  54. }
  55. surface->getCanvas()->clear(0);
  56. surface->getCanvas()->drawPicture(pic);
  57. return surface->makeImageSnapshot();
  58. }
  59. static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
  60. return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
  61. SkImage::BitDepth::kU8,
  62. SkColorSpace::MakeSRGB());
  63. }
  64. static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
  65. sk_sp<SkImage> src(make_raster(ctx, pic, info));
  66. if (!src) {
  67. return nullptr;
  68. }
  69. sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100);
  70. if (!encoded) {
  71. return nullptr;
  72. }
  73. return SkImage::MakeFromEncoded(std::move(encoded));
  74. }
  75. const ImageMakerProc gProcs[] = {
  76. make_raster,
  77. make_texture,
  78. make_pict_gen,
  79. make_encode_gen,
  80. };
  81. /*
  82. * Exercise drawing pictures inside an image, showing that the image version is pixelated
  83. * (correctly) when it is inside an image.
  84. */
  85. class ImageShaderGM : public skiagm::GM {
  86. sk_sp<SkPicture> fPicture;
  87. public:
  88. ImageShaderGM() {}
  89. protected:
  90. SkString onShortName() override {
  91. return SkString("image-shader");
  92. }
  93. SkISize onISize() override {
  94. return SkISize::Make(850, 450);
  95. }
  96. void onOnceBeforeDraw() override {
  97. const SkRect bounds = SkRect::MakeWH(100, 100);
  98. SkPictureRecorder recorder;
  99. draw_something(recorder.beginRecording(bounds), bounds);
  100. fPicture = recorder.finishRecordingAsPicture();
  101. }
  102. void testImage(SkCanvas* canvas, SkImage* image) {
  103. SkAutoCanvasRestore acr(canvas, true);
  104. canvas->drawImage(image, 0, 0);
  105. canvas->translate(0, 120);
  106. const SkTileMode tile = SkTileMode::kRepeat;
  107. const SkMatrix localM = SkMatrix::MakeTrans(-50, -50);
  108. SkPaint paint;
  109. paint.setShader(image->makeShader(tile, tile, &localM));
  110. paint.setAntiAlias(true);
  111. canvas->drawCircle(50, 50, 50, paint);
  112. }
  113. void onDraw(SkCanvas* canvas) override {
  114. canvas->translate(20, 20);
  115. const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
  116. for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
  117. sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info));
  118. if (image) {
  119. this->testImage(canvas, image.get());
  120. }
  121. canvas->translate(120, 0);
  122. }
  123. }
  124. private:
  125. typedef skiagm::GM INHERITED;
  126. };
  127. DEF_GM( return new ImageShaderGM; )