• 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 "SkImage.h"
9 #include "SkImageGenerator.h"
10 #include "SkNextID.h"
11 #include "SkYUVAIndex.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 "GrTextureProxy.h"
63 
generateTexture(GrContext * ctx,const SkImageInfo & info,const SkIPoint & origin,bool willNeedMipMaps)64 sk_sp<GrTextureProxy> SkImageGenerator::generateTexture(GrContext* ctx, const SkImageInfo& info,
65                                                         const SkIPoint& origin,
66                                                         bool willNeedMipMaps) {
67     SkIRect srcRect = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height());
68     if (!SkIRect::MakeWH(fInfo.width(), fInfo.height()).contains(srcRect)) {
69         return nullptr;
70     }
71     return this->onGenerateTexture(ctx, info, origin, willNeedMipMaps);
72 }
73 
onGenerateTexture(GrContext *,const SkImageInfo &,const SkIPoint &,bool willNeedMipMaps)74 sk_sp<GrTextureProxy> SkImageGenerator::onGenerateTexture(GrContext*, const SkImageInfo&,
75                                                           const SkIPoint&,
76                                                           bool willNeedMipMaps) {
77     return nullptr;
78 }
79 #endif
80 
81 ///////////////////////////////////////////////////////////////////////////////////////////////////
82 
83 #include "SkBitmap.h"
84 #include "SkColorTable.h"
85 
86 #include "SkGraphics.h"
87 
88 static SkGraphics::ImageGeneratorFromEncodedDataFactory gFactory;
89 
90 SkGraphics::ImageGeneratorFromEncodedDataFactory
SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory)91 SkGraphics::SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory)
92 {
93     ImageGeneratorFromEncodedDataFactory prev = gFactory;
94     gFactory = factory;
95     return prev;
96 }
97 
MakeFromEncoded(sk_sp<SkData> data)98 std::unique_ptr<SkImageGenerator> SkImageGenerator::MakeFromEncoded(sk_sp<SkData> data) {
99     if (!data) {
100         return nullptr;
101     }
102     if (gFactory) {
103         if (std::unique_ptr<SkImageGenerator> generator = gFactory(data)) {
104             return generator;
105         }
106     }
107     return SkImageGenerator::MakeFromEncodedImpl(std::move(data));
108 }
109