xref: /aosp_15_r20/external/skia/src/image/SkPictureImageGenerator.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "src/image/SkPictureImageGenerator.h"
9 
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageGenerator.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkPicture.h"
20 #include "include/core/SkSize.h"
21 #include "src/base/SkTLazy.h"
22 #include "src/image/SkImageGeneratorPriv.h"
23 
24 #include <memory>
25 #include <utility>
26 
27 namespace SkImageGenerators {
MakeFromPicture(const SkISize & size,sk_sp<SkPicture> picture,const SkMatrix * matrix,const SkPaint * paint,SkImages::BitDepth bitDepth,sk_sp<SkColorSpace> colorSpace)28 std::unique_ptr<SkImageGenerator> MakeFromPicture(
29         const SkISize& size,
30         sk_sp<SkPicture> picture,
31         const SkMatrix* matrix,
32         const SkPaint* paint,
33         SkImages::BitDepth bitDepth,
34         sk_sp<SkColorSpace> colorSpace) {
35     return MakeFromPicture(size, picture, matrix, paint, bitDepth, colorSpace, {});
36 }
37 
MakeFromPicture(const SkISize & size,sk_sp<SkPicture> picture,const SkMatrix * matrix,const SkPaint * paint,SkImages::BitDepth bitDepth,sk_sp<SkColorSpace> colorSpace,SkSurfaceProps props)38 std::unique_ptr<SkImageGenerator> MakeFromPicture(const SkISize& size,
39                                                   sk_sp<SkPicture> picture,
40                                                   const SkMatrix* matrix,
41                                                   const SkPaint* paint,
42                                                   SkImages::BitDepth bitDepth,
43                                                   sk_sp<SkColorSpace> colorSpace,
44                                                   SkSurfaceProps props) {
45     if (!picture || !colorSpace || size.isEmpty()) {
46         return nullptr;
47     }
48 
49     SkColorType colorType = kN32_SkColorType;
50     if (SkImages::BitDepth::kF16 == bitDepth) {
51         colorType = kRGBA_F16_SkColorType;
52     }
53 
54     SkImageInfo info =
55             SkImageInfo::Make(size, colorType, kPremul_SkAlphaType, std::move(colorSpace));
56     return std::unique_ptr<SkImageGenerator>(
57         new SkPictureImageGenerator(info, std::move(picture), matrix, paint, props));
58 }
59 } // SkImageGenerators
60 
61 ///////////////////////////////////////////////////////////////////////////////////////////////////
62 
SkPictureImageGenerator(const SkImageInfo & info,sk_sp<SkPicture> picture,const SkMatrix * matrix,const SkPaint * paint,const SkSurfaceProps & props)63 SkPictureImageGenerator::SkPictureImageGenerator(const SkImageInfo& info, sk_sp<SkPicture> picture,
64                                                  const SkMatrix* matrix, const SkPaint* paint,
65                                                  const SkSurfaceProps& props)
66         : SkImageGenerator(info)
67         , fPicture(std::move(picture))
68         , fProps(props) {
69 
70     if (matrix) {
71         fMatrix = *matrix;
72     } else {
73         fMatrix.reset();
74     }
75 
76     if (paint) {
77         fPaint.set(*paint);
78     }
79 }
80 
onGetPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options & opts)81 bool SkPictureImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
82                                           const Options& opts) {
83     std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, pixels, rowBytes, &fProps);
84     if (!canvas) {
85         return false;
86     }
87     canvas->clear(0);
88     canvas->drawPicture(fPicture, &fMatrix, fPaint.getMaybeNull());
89     return true;
90 }
91