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