SkPictureImageGenerator.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "include/core/SkCanvas.h"
  8. #include "include/core/SkImageGenerator.h"
  9. #include "include/core/SkMatrix.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPicture.h"
  12. #include "include/core/SkSurface.h"
  13. #include "src/core/SkMakeUnique.h"
  14. #include "src/core/SkTLazy.h"
  15. #include "src/image/SkImage_Base.h"
  16. class SkPictureImageGenerator : public SkImageGenerator {
  17. public:
  18. SkPictureImageGenerator(const SkImageInfo& info, sk_sp<SkPicture>, const SkMatrix*,
  19. const SkPaint*);
  20. protected:
  21. bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options& opts)
  22. override;
  23. #if SK_SUPPORT_GPU
  24. TexGenType onCanGenerateTexture() const override { return TexGenType::kExpensive; }
  25. sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
  26. const SkIPoint&, bool willNeedMipMaps) override;
  27. #endif
  28. private:
  29. sk_sp<SkPicture> fPicture;
  30. SkMatrix fMatrix;
  31. SkTLazy<SkPaint> fPaint;
  32. typedef SkImageGenerator INHERITED;
  33. };
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////
  35. std::unique_ptr<SkImageGenerator>
  36. SkImageGenerator::MakeFromPicture(const SkISize& size, sk_sp<SkPicture> picture,
  37. const SkMatrix* matrix, const SkPaint* paint,
  38. SkImage::BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
  39. if (!picture || !colorSpace || size.isEmpty()) {
  40. return nullptr;
  41. }
  42. SkColorType colorType = kN32_SkColorType;
  43. if (SkImage::BitDepth::kF16 == bitDepth) {
  44. colorType = kRGBA_F16_SkColorType;
  45. }
  46. SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType,
  47. kPremul_SkAlphaType, std::move(colorSpace));
  48. return std::unique_ptr<SkImageGenerator>(
  49. new SkPictureImageGenerator(info, std::move(picture), matrix, paint));
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////
  52. SkPictureImageGenerator::SkPictureImageGenerator(const SkImageInfo& info, sk_sp<SkPicture> picture,
  53. const SkMatrix* matrix, const SkPaint* paint)
  54. : INHERITED(info)
  55. , fPicture(std::move(picture)) {
  56. if (matrix) {
  57. fMatrix = *matrix;
  58. } else {
  59. fMatrix.reset();
  60. }
  61. if (paint) {
  62. fPaint.set(*paint);
  63. }
  64. }
  65. bool SkPictureImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
  66. const Options& opts) {
  67. SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
  68. std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, pixels, rowBytes, &props);
  69. if (!canvas) {
  70. return false;
  71. }
  72. canvas->clear(0);
  73. canvas->drawPicture(fPicture, &fMatrix, fPaint.getMaybeNull());
  74. return true;
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////////////////////////
  77. #if SK_SUPPORT_GPU
  78. #include "include/private/GrRecordingContext.h"
  79. #include "src/gpu/GrRecordingContextPriv.h"
  80. sk_sp<GrTextureProxy> SkPictureImageGenerator::onGenerateTexture(
  81. GrRecordingContext* ctx, const SkImageInfo& info,
  82. const SkIPoint& origin, bool willNeedMipMaps) {
  83. SkASSERT(ctx);
  84. SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
  85. // CONTEXT TODO: remove this use of 'backdoor' to create an SkSkSurface
  86. sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx->priv().backdoor(),
  87. SkBudgeted::kYes, info, 0,
  88. kTopLeft_GrSurfaceOrigin, &props,
  89. willNeedMipMaps));
  90. if (!surface) {
  91. return nullptr;
  92. }
  93. SkMatrix matrix = fMatrix;
  94. matrix.postTranslate(-origin.x(), -origin.y());
  95. surface->getCanvas()->clear(0);
  96. surface->getCanvas()->drawPicture(fPicture.get(), &matrix, fPaint.getMaybeNull());
  97. sk_sp<SkImage> image(surface->makeImageSnapshot());
  98. if (!image) {
  99. return nullptr;
  100. }
  101. sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(ctx);
  102. SkASSERT(!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped());
  103. return proxy;
  104. }
  105. #endif