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