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/SkImage.h" |
9 | #include "include/core/SkImageGenerator.h" |
10 | #include "include/core/SkYUVAIndex.h" |
11 | #include "src/core/SkNextID.h" |
12 | |
13 | #if SK_SUPPORT_GPU |
14 | #include "include/gpu/GrDirectContext.h" |
15 | #endif |
16 | |
17 | SkImageGenerator::SkImageGenerator(const SkImageInfo& info, uint32_t uniqueID) |
18 | : fInfo(info) |
19 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) |
20 | {} |
21 | |
22 | bool SkImageGenerator::isValid(GrContext* context) const { |
23 | #if SK_SUPPORT_GPU |
24 | return this->isValid(static_cast<GrRecordingContext*>(context)); |
25 | #else |
26 | if (context) { |
27 | return false; |
28 | } |
29 | return this->isValid(static_cast<GrRecordingContext*>(nullptr)); |
30 | #endif |
31 | } |
32 | |
33 | bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
34 | if (kUnknown_SkColorType == info.colorType()) { |
35 | return false; |
36 | } |
37 | if (nullptr == pixels) { |
38 | return false; |
39 | } |
40 | if (rowBytes < info.minRowBytes()) { |
41 | return false; |
42 | } |
43 | |
44 | Options defaultOpts; |
45 | return this->onGetPixels(info, pixels, rowBytes, defaultOpts); |
46 | } |
47 | |
48 | bool SkImageGenerator::queryYUVA8(SkYUVASizeInfo* sizeInfo, |
49 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
50 | SkYUVColorSpace* colorSpace) const { |
51 | SkASSERT(sizeInfo); |
52 | |
53 | return this->onQueryYUVA8(sizeInfo, yuvaIndices, colorSpace); |
54 | } |
55 | |
56 | bool SkImageGenerator::getYUVA8Planes(const SkYUVASizeInfo& sizeInfo, |
57 | const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
58 | void* planes[SkYUVASizeInfo::kMaxCount]) { |
59 | |
60 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
61 | SkASSERT(sizeInfo.fSizes[i].fWidth >= 0); |
62 | SkASSERT(sizeInfo.fSizes[i].fHeight >= 0); |
63 | SkASSERT(sizeInfo.fWidthBytes[i] >= (size_t) sizeInfo.fSizes[i].fWidth); |
64 | } |
65 | |
66 | int numPlanes = 0; |
67 | SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &numPlanes)); |
68 | SkASSERT(planes); |
69 | for (int i = 0; i < numPlanes; ++i) { |
70 | SkASSERT(planes[i]); |
71 | } |
72 | |
73 | return this->onGetYUVA8Planes(sizeInfo, yuvaIndices, planes); |
74 | } |
75 | |
76 | #if SK_SUPPORT_GPU |
77 | #include "src/gpu/GrSurfaceProxyView.h" |
78 | |
79 | GrSurfaceProxyView SkImageGenerator::generateTexture(GrRecordingContext* ctx, |
80 | const SkImageInfo& info, |
81 | const SkIPoint& origin, |
82 | GrMipmapped mipMapped, |
83 | GrImageTexGenPolicy texGenPolicy) { |
84 | SkIRect srcRect = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()); |
85 | if (!SkIRect::MakeWH(fInfo.width(), fInfo.height()).contains(srcRect)) { |
86 | return {}; |
87 | } |
88 | return this->onGenerateTexture(ctx, info, origin, mipMapped, texGenPolicy); |
89 | } |
90 | |
91 | GrSurfaceProxyView SkImageGenerator::onGenerateTexture(GrRecordingContext*, |
92 | const SkImageInfo&, |
93 | const SkIPoint&, |
94 | GrMipmapped, |
95 | GrImageTexGenPolicy) { |
96 | return {}; |
97 | } |
98 | #endif |
99 | |
100 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
101 | |
102 | #include "include/core/SkBitmap.h" |
103 | #include "src/codec/SkColorTable.h" |
104 | |
105 | #include "include/core/SkGraphics.h" |
106 | |
107 | static SkGraphics::ImageGeneratorFromEncodedDataFactory gFactory; |
108 | |
109 | SkGraphics::ImageGeneratorFromEncodedDataFactory |
110 | SkGraphics::SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory) |
111 | { |
112 | ImageGeneratorFromEncodedDataFactory prev = gFactory; |
113 | gFactory = factory; |
114 | return prev; |
115 | } |
116 | |
117 | std::unique_ptr<SkImageGenerator> SkImageGenerator::MakeFromEncoded(sk_sp<SkData> data) { |
118 | if (!data) { |
119 | return nullptr; |
120 | } |
121 | if (gFactory) { |
122 | if (std::unique_ptr<SkImageGenerator> generator = gFactory(data)) { |
123 | return generator; |
124 | } |
125 | } |
126 | return SkImageGenerator::MakeFromEncodedImpl(std::move(data)); |
127 | } |
128 | |