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(GrRecordingContext*, const SkImageInfo&,
30                                             const SkIPoint&, 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
95 #include "GrRecordingContext.h"
96 #include "GrRecordingContextPriv.h"
97 
onGenerateTexture(GrRecordingContext * ctx,const SkImageInfo & info,const SkIPoint & origin,bool willNeedMipMaps)98 sk_sp<GrTextureProxy> SkPictureImageGenerator::onGenerateTexture(
99         GrRecordingContext* ctx, const SkImageInfo& info,
100         const SkIPoint& origin, bool willNeedMipMaps) {
101     SkASSERT(ctx);
102 
103     SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
104 
105     // CONTEXT TODO: remove this use of 'backdoor' to create an SkSkSurface
106     sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx->priv().backdoor(),
107                                                          SkBudgeted::kYes, info, 0,
108                                                          kTopLeft_GrSurfaceOrigin, &props,
109                                                          willNeedMipMaps));
110     if (!surface) {
111         return nullptr;
112     }
113 
114     SkMatrix matrix = fMatrix;
115     matrix.postTranslate(-origin.x(), -origin.y());
116     surface->getCanvas()->clear(0);
117     surface->getCanvas()->drawPicture(fPicture.get(), &matrix, fPaint.getMaybeNull());
118     sk_sp<SkImage> image(surface->makeImageSnapshot());
119     if (!image) {
120         return nullptr;
121     }
122     sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(ctx);
123     SkASSERT(!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped());
124     return proxy;
125 }
126 #endif
127