• 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 
SkImageGenerator(const SkImageInfo & info,uint32_t uniqueID)12 SkImageGenerator::SkImageGenerator(const SkImageInfo& info, uint32_t uniqueID)
13     : fInfo(info)
14     , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID)
15 {}
16 
getPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options * opts)17 bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
18                                  const Options* opts) {
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     if (!opts) {
31         opts = &defaultOpts;
32     }
33     return this->onGetPixels(info, pixels, rowBytes, *opts);
34 }
35 
getPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)36 bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
37     return this->getPixels(info, pixels, rowBytes, nullptr);
38 }
39 
queryYUV8(SkYUVSizeInfo * sizeInfo,SkYUVColorSpace * colorSpace) const40 bool SkImageGenerator::queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
41     SkASSERT(sizeInfo);
42 
43     return this->onQueryYUV8(sizeInfo, colorSpace);
44 }
45 
getYUV8Planes(const SkYUVSizeInfo & sizeInfo,void * planes[3])46 bool SkImageGenerator::getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
47     SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth >= 0);
48     SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight >= 0);
49     SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kU].fWidth >= 0);
50     SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight >= 0);
51     SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kV].fWidth >= 0);
52     SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kV].fHeight >= 0);
53     SkASSERT(sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] >=
54             (size_t) sizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth);
55     SkASSERT(sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] >=
56             (size_t) sizeInfo.fSizes[SkYUVSizeInfo::kU].fWidth);
57     SkASSERT(sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] >=
58             (size_t) sizeInfo.fSizes[SkYUVSizeInfo::kV].fWidth);
59     SkASSERT(planes && planes[0] && planes[1] && planes[2]);
60 
61     return this->onGetYUV8Planes(sizeInfo, planes);
62 }
63 
64 #if SK_SUPPORT_GPU
65 #include "GrTextureProxy.h"
66 
generateTexture(GrContext * ctx,const SkImageInfo & info,const SkIPoint & origin,SkTransferFunctionBehavior behavior)67 sk_sp<GrTextureProxy> SkImageGenerator::generateTexture(GrContext* ctx, const SkImageInfo& info,
68                                                         const SkIPoint& origin,
69                                                         SkTransferFunctionBehavior behavior) {
70     SkIRect srcRect = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height());
71     if (!SkIRect::MakeWH(fInfo.width(), fInfo.height()).contains(srcRect)) {
72         return nullptr;
73     }
74     return this->onGenerateTexture(ctx, info, origin, behavior);
75 }
76 
onGenerateTexture(GrContext *,const SkImageInfo &,const SkIPoint &,SkTransferFunctionBehavior)77 sk_sp<GrTextureProxy> SkImageGenerator::onGenerateTexture(GrContext*, const SkImageInfo&,
78                                                           const SkIPoint&,
79                                                           SkTransferFunctionBehavior) {
80     return nullptr;
81 }
82 #endif
83 
84 ///////////////////////////////////////////////////////////////////////////////////////////////////
85 
86 #include "SkBitmap.h"
87 #include "SkColorTable.h"
88 
89 #include "SkGraphics.h"
90 
91 static SkGraphics::ImageGeneratorFromEncodedDataFactory gFactory;
92 
93 SkGraphics::ImageGeneratorFromEncodedDataFactory
SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory)94 SkGraphics::SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory)
95 {
96     ImageGeneratorFromEncodedDataFactory prev = gFactory;
97     gFactory = factory;
98     return prev;
99 }
100 
MakeFromEncoded(sk_sp<SkData> data)101 std::unique_ptr<SkImageGenerator> SkImageGenerator::MakeFromEncoded(sk_sp<SkData> data) {
102     if (!data) {
103         return nullptr;
104     }
105     if (gFactory) {
106         if (std::unique_ptr<SkImageGenerator> generator = gFactory(data)) {
107             return generator;
108         }
109     }
110     return SkImageGenerator::MakeFromEncodedImpl(std::move(data));
111 }
112