| 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 | SkImageGenerator::SkImageGenerator(const SkImageInfo& info, uint32_t uniqueID) |
| 14 | : fInfo(info) |
| 15 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) |
| 16 | {} |
| 17 | |
| 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 | |
| 33 | 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 | |
| 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/GrSurfaceProxyView.h" |
| 63 | |
| 64 | GrSurfaceProxyView SkImageGenerator::generateTexture(GrRecordingContext* ctx, |
| 65 | const SkImageInfo& info, |
| 66 | const SkIPoint& origin, |
| 67 | GrMipMapped mipMapped, |
| 68 | GrImageTexGenPolicy texGenPolicy) { |
| 69 | SkIRect srcRect = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()); |
| 70 | if (!SkIRect::MakeWH(fInfo.width(), fInfo.height()).contains(srcRect)) { |
| 71 | return {}; |
| 72 | } |
| 73 | return this->onGenerateTexture(ctx, info, origin, mipMapped, texGenPolicy); |
| 74 | } |
| 75 | |
| 76 | GrSurfaceProxyView SkImageGenerator::onGenerateTexture(GrRecordingContext*, |
| 77 | const SkImageInfo&, |
| 78 | const SkIPoint&, |
| 79 | GrMipMapped, |
| 80 | GrImageTexGenPolicy) { |
| 81 | return {}; |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 86 | |
| 87 | #include "include/core/SkBitmap.h" |
| 88 | #include "src/codec/SkColorTable.h" |
| 89 | |
| 90 | #include "include/core/SkGraphics.h" |
| 91 | |
| 92 | static SkGraphics::ImageGeneratorFromEncodedDataFactory gFactory; |
| 93 | |
| 94 | SkGraphics::ImageGeneratorFromEncodedDataFactory |
| 95 | SkGraphics::SetImageGeneratorFromEncodedDataFactory(ImageGeneratorFromEncodedDataFactory factory) |
| 96 | { |
| 97 | ImageGeneratorFromEncodedDataFactory prev = gFactory; |
| 98 | gFactory = factory; |
| 99 | return prev; |
| 100 | } |
| 101 | |
| 102 | std::unique_ptr<SkImageGenerator> SkImageGenerator::MakeFromEncoded(sk_sp<SkData> data) { |
| 103 | if (!data) { |
| 104 | return nullptr; |
| 105 | } |
| 106 | if (gFactory) { |
| 107 | if (std::unique_ptr<SkImageGenerator> generator = gFactory(data)) { |
| 108 | return generator; |
| 109 | } |
| 110 | } |
| 111 | return SkImageGenerator::MakeFromEncodedImpl(std::move(data)); |
| 112 | } |
| 113 | |