SkImageGenerator.cpp revision ef004e1b49ce6ad488ea4444c0eb896ef37c1242
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 "SkImageGenerator.h"
9
10bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
11                                 SkPMColor ctable[], int* ctableCount) {
12    if (kUnknown_SkColorType == info.colorType()) {
13        return false;
14    }
15    if (NULL == pixels) {
16        return false;
17    }
18    if (rowBytes < info.minRowBytes()) {
19        return false;
20    }
21
22    if (kIndex_8_SkColorType == info.colorType()) {
23        if (NULL == ctable || NULL == ctableCount) {
24            return false;
25        }
26    } else {
27        if (ctableCount) {
28            *ctableCount = 0;
29        }
30        ctableCount = NULL;
31        ctable = NULL;
32    }
33
34    const bool success = this->onGetPixels(info, pixels, rowBytes, ctable, ctableCount);
35    if (success && ctableCount) {
36        SkASSERT(*ctableCount >= 0 && *ctableCount <= 256);
37    }
38    return success;
39}
40
41bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
42    SkASSERT(kIndex_8_SkColorType != info.colorType());
43    if (kIndex_8_SkColorType == info.colorType()) {
44        return false;
45    }
46    return this->getPixels(info, pixels, rowBytes, NULL, NULL);
47}
48
49bool SkImageGenerator::getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
50                                     SkYUVColorSpace* colorSpace) {
51#ifdef SK_DEBUG
52    // In all cases, we need the sizes array
53    SkASSERT(sizes);
54
55    bool isValidWithPlanes = (planes) && (rowBytes) &&
56        ((planes[0]) && (planes[1]) && (planes[2]) &&
57         (0  != rowBytes[0]) && (0  != rowBytes[1]) && (0  != rowBytes[2]));
58    bool isValidWithoutPlanes =
59        ((NULL == planes) ||
60         ((NULL == planes[0]) && (NULL == planes[1]) && (NULL == planes[2]))) &&
61        ((NULL == rowBytes) ||
62         ((0 == rowBytes[0]) && (0 == rowBytes[1]) && (0 == rowBytes[2])));
63
64    // Either we have all planes and rowBytes information or we have none of it
65    // Having only partial information is not supported
66    SkASSERT(isValidWithPlanes || isValidWithoutPlanes);
67
68    // If we do have planes information, make sure all sizes are non 0
69    // and all rowBytes are valid
70    SkASSERT(!isValidWithPlanes ||
71             ((sizes[0].fWidth  >= 0) &&
72              (sizes[0].fHeight >= 0) &&
73              (sizes[1].fWidth  >= 0) &&
74              (sizes[1].fHeight >= 0) &&
75              (sizes[2].fWidth  >= 0) &&
76              (sizes[2].fHeight >= 0) &&
77              (rowBytes[0] >= (size_t)sizes[0].fWidth) &&
78              (rowBytes[1] >= (size_t)sizes[1].fWidth) &&
79              (rowBytes[2] >= (size_t)sizes[2].fWidth)));
80#endif
81
82    return this->onGetYUV8Planes(sizes, planes, rowBytes, colorSpace);
83}
84
85bool SkImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3]) {
86    return false;
87}
88
89bool SkImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
90                                       SkYUVColorSpace* colorSpace) {
91    // In order to maintain compatibility with clients that implemented the original
92    // onGetYUV8Planes interface, we assume that the color space is JPEG.
93    // TODO(rileya): remove this and the old onGetYUV8Planes once clients switch over to
94    // the new interface.
95    if (colorSpace) {
96        *colorSpace = kJPEG_SkYUVColorSpace;
97    }
98    return this->onGetYUV8Planes(sizes, planes, rowBytes);
99}
100
101/////////////////////////////////////////////////////////////////////////////////////////////
102
103SkData* SkImageGenerator::onRefEncodedData() {
104    return NULL;
105}
106
107bool SkImageGenerator::onGetPixels(const SkImageInfo& info, void* dst, size_t rb,
108                                   SkPMColor* colors, int* colorCount) {
109    return false;
110}
111
112///////////////////////////////////////////////////////////////////////////////////////////////////
113
114#include "SkGraphics.h"
115
116static SkGraphics::ImageGeneratorFromEncodedFactory gFactory;
117
118SkGraphics::ImageGeneratorFromEncodedFactory
119SkGraphics::SetImageGeneratorFromEncodedFactory(ImageGeneratorFromEncodedFactory factory)
120{
121    ImageGeneratorFromEncodedFactory prev = gFactory;
122    gFactory = factory;
123    return prev;
124}
125
126SkImageGenerator* SkImageGenerator::NewFromEncoded(SkData* data) {
127    if (NULL == data) {
128        return NULL;
129    }
130    if (gFactory) {
131        if (SkImageGenerator* generator = gFactory(data)) {
132            return generator;
133        }
134    }
135    return SkImageGenerator::NewFromEncodedImpl(data);
136}
137