1 /*
2 * Copyright 2014 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 "include/core/SkImageGenerator.h"
9
10 #include "include/core/SkImage.h"
11 #include "src/core/SkNextID.h"
12
13 #if defined(SK_GANESH)
14 #include "include/gpu/GrRecordingContext.h"
15 #endif
16
SkImageGenerator(const SkImageInfo & info,uint32_t uniqueID)17 SkImageGenerator::SkImageGenerator(const SkImageInfo& info, uint32_t uniqueID)
18 : fInfo(info)
19 , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID)
20 {}
21
getPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)22 bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
23 if (kUnknown_SkColorType == info.colorType()) {
24 return false;
25 }
26 if (nullptr == pixels) {
27 return false;
28 }
29 if (rowBytes < info.minRowBytes()) {
30 return false;
31 }
32
33 Options defaultOpts;
34 return this->onGetPixels(info, pixels, rowBytes, defaultOpts);
35 }
36
queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes & supportedDataTypes,SkYUVAPixmapInfo * yuvaPixmapInfo) const37 bool SkImageGenerator::queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
38 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
39 SkASSERT(yuvaPixmapInfo);
40
41 return this->onQueryYUVAInfo(supportedDataTypes, yuvaPixmapInfo) &&
42 yuvaPixmapInfo->isSupported(supportedDataTypes);
43 }
44
getYUVAPlanes(const SkYUVAPixmaps & yuvaPixmaps)45 bool SkImageGenerator::getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
46 return this->onGetYUVAPlanes(yuvaPixmaps);
47 }
48
49 #if defined(SK_GANESH)
50 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
51
generateTexture(GrRecordingContext * ctx,const SkImageInfo & info,GrMipmapped mipmapped,GrImageTexGenPolicy texGenPolicy)52 GrSurfaceProxyView SkImageGenerator::generateTexture(GrRecordingContext* ctx,
53 const SkImageInfo& info,
54 GrMipmapped mipmapped,
55 GrImageTexGenPolicy texGenPolicy) {
56 SkASSERT_RELEASE(fInfo.dimensions() == info.dimensions());
57
58 if (!ctx || ctx->abandoned()) {
59 return {};
60 }
61
62 return this->onGenerateTexture(ctx, info, mipmapped, texGenPolicy);
63 }
64
onGenerateTexture(GrRecordingContext *,const SkImageInfo &,GrMipmapped,GrImageTexGenPolicy)65 GrSurfaceProxyView SkImageGenerator::onGenerateTexture(GrRecordingContext*,
66 const SkImageInfo&,
67 GrMipmapped,
68 GrImageTexGenPolicy) {
69 return {};
70 }
71 #endif // defined(SK_GANESH)
72
73 #if SK_GRAPHITE
74 #include "src/gpu/graphite/Image_Graphite.h"
75
makeTextureImage(skgpu::graphite::Recorder * recorder,const SkImageInfo & info,skgpu::Mipmapped mipmapped)76 sk_sp<SkImage> SkImageGenerator::makeTextureImage(skgpu::graphite::Recorder* recorder,
77 const SkImageInfo& info,
78 skgpu::Mipmapped mipmapped) {
79 // This still allows for a difference in colorType and colorSpace. Just no subsetting.
80 if (fInfo.dimensions() != info.dimensions()) {
81 return nullptr;
82 }
83
84 return this->onMakeTextureImage(recorder, info, mipmapped);
85 }
86
onMakeTextureImage(skgpu::graphite::Recorder *,const SkImageInfo &,skgpu::Mipmapped)87 sk_sp<SkImage> SkImageGenerator::onMakeTextureImage(skgpu::graphite::Recorder*,
88 const SkImageInfo&,
89 skgpu::Mipmapped) {
90 return nullptr;
91 }
92
93 #endif // SK_GRAPHITE
94
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 #include "include/core/SkBitmap.h"
98 #include "src/codec/SkColorTable.h"
99
100 #include "include/core/SkGraphics.h"
101
102 static SkGraphics::ImageGeneratorFromEncodedDataFactory gFactory;
103
104 SkGraphics::ImageGeneratorFromEncodedDataFactory
SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory)105 SkGraphics::SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory)
106 {
107 ImageGeneratorFromEncodedDataFactory prev = gFactory;
108 gFactory = factory;
109 return prev;
110 }
111
MakeFromEncoded(sk_sp<SkData> data,std::optional<SkAlphaType> at)112 std::unique_ptr<SkImageGenerator> SkImageGenerator::MakeFromEncoded(
113 sk_sp<SkData> data, std::optional<SkAlphaType> at) {
114 if (!data || at == kOpaque_SkAlphaType) {
115 return nullptr;
116 }
117 if (gFactory) {
118 if (std::unique_ptr<SkImageGenerator> generator = gFactory(data)) {
119 return generator;
120 }
121 }
122 return SkImageGenerator::MakeFromEncodedImpl(std::move(data), at);
123 }
124