| 1 |
/* |
| 2 |
* Copyright 2015 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 |
#ifndef SkCodec_DEFINED |
| 9 |
#define SkCodec_DEFINED |
| 10 |
|
| 11 |
#include "include/codec/SkEncodedOrigin.h" |
| 12 |
#include "include/core/SkImageInfo.h" |
| 13 |
#include "include/core/SkPixmap.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkSpan.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/core/SkYUVAPixmaps.h" |
| 20 |
#include "include/private/SkEncodedInfo.h" |
| 21 |
#include "include/private/SkHdrMetadata.h" |
| 22 |
#include "include/private/base/SkNoncopyable.h" |
| 23 |
#include "modules/skcms/skcms.h" |
| 24 |
|
| 25 |
#include <cstddef> |
| 26 |
#include <functional> |
| 27 |
#include <memory> |
| 28 |
#include <optional> |
| 29 |
#include <string_view> |
| 30 |
#include <tuple> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
class SkData; |
| 34 |
class SkFrameHolder; |
| 35 |
class SkImage; |
| 36 |
class SkPngChunkReader; |
| 37 |
class SkSampler; |
| 38 |
class SkStream; |
| 39 |
struct SkGainmapInfo; |
| 40 |
enum SkAlphaType : int; |
| 41 |
enum class SkEncodedImageFormat; |
| 42 |
|
| 43 |
namespace SkCodecAnimation { |
| 44 |
enum class Blend; |
| 45 |
enum class DisposalMethod; |
| 46 |
} |
| 47 |
|
| 48 |
namespace DM { |
| 49 |
class CodecSrc; |
| 50 |
} // namespace DM |
| 51 |
|
| 52 |
namespace SkCodecs { |
| 53 |
struct Decoder; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Abstraction layer directly on top of an image codec. |
| 58 |
*/ |
| 59 |
class SK_API SkCodec : SkNoncopyable { |
| 60 |
public: |
| 61 |
/** |
| 62 |
* Minimum number of bytes that must be buffered in SkStream input. |
| 63 |
* |
| 64 |
* An SkStream passed to NewFromStream must be able to use this many |
| 65 |
* bytes to determine the image type. Then the same SkStream must be |
| 66 |
* passed to the correct decoder to read from the beginning. |
| 67 |
* |
| 68 |
* This can be accomplished by implementing peek() to support peeking |
| 69 |
* this many bytes, or by implementing rewind() to be able to rewind() |
| 70 |
* after reading this many bytes. |
| 71 |
*/ |
| 72 |
static constexpr size_t MinBufferedBytesNeeded() { return 32; } |
| 73 |
|
| 74 |
/** |
| 75 |
* Error codes for various SkCodec methods. |
| 76 |
*/ |
| 77 |
enum Result { |
| 78 |
/** |
| 79 |
* General return value for success. |
| 80 |
*/ |
| 81 |
kSuccess, |
| 82 |
/** |
| 83 |
* The input is incomplete. A partial image was generated. |
| 84 |
*/ |
| 85 |
kIncompleteInput, |
| 86 |
/** |
| 87 |
* Like kIncompleteInput, except the input had an error. |
| 88 |
* |
| 89 |
* If returned from an incremental decode, decoding cannot continue, |
| 90 |
* even with more data. |
| 91 |
*/ |
| 92 |
kErrorInInput, |
| 93 |
/** |
| 94 |
* The generator cannot convert to match the request, ignoring |
| 95 |
* dimensions. |
| 96 |
*/ |
| 97 |
kInvalidConversion, |
| 98 |
/** |
| 99 |
* The generator cannot scale to requested size. |
| 100 |
*/ |
| 101 |
kInvalidScale, |
| 102 |
/** |
| 103 |
* Parameters (besides info) are invalid. e.g. NULL pixels, rowBytes |
| 104 |
* too small, etc. |
| 105 |
*/ |
| 106 |
kInvalidParameters, |
| 107 |
/** |
| 108 |
* The input did not contain a valid image. |
| 109 |
*/ |
| 110 |
kInvalidInput, |
| 111 |
/** |
| 112 |
* Fulfilling this request requires rewinding the input, which is not |
| 113 |
* supported for this input. |
| 114 |
*/ |
| 115 |
kCouldNotRewind, |
| 116 |
/** |
| 117 |
* An internal error, such as OOM. |
| 118 |
*/ |
| 119 |
kInternalError, |
| 120 |
/** |
| 121 |
* This method is not implemented by this codec. |
| 122 |
* FIXME: Perhaps this should be kUnsupported? |
| 123 |
*/ |
| 124 |
kUnimplemented, |
| 125 |
/** |
| 126 |
* If the memory allocation exceeded the provided budget. |
| 127 |
*/ |
| 128 |
kOutOfMemory, |
| 129 |
}; |
| 130 |
|
| 131 |
/** |
| 132 |
* Readable string representing the error code. |
| 133 |
*/ |
| 134 |
static const char* ResultToString(Result); |
| 135 |
|
| 136 |
/** |
| 137 |
* For container formats that contain both still images and image sequences, |
| 138 |
* instruct the decoder how the output should be selected. (Refer to comments |
| 139 |
* for each value for more details.) |
| 140 |
*/ |
| 141 |
enum class SelectionPolicy { |
| 142 |
/** |
| 143 |
* If the container format contains both still images and image sequences, |
| 144 |
* SkCodec should choose one of the still images. This is the default. |
| 145 |
* Note that kPreferStillImage may prevent use of the animation features |
| 146 |
* if the input is not rewindable. |
| 147 |
*/ |
| 148 |
kPreferStillImage, |
| 149 |
/** |
| 150 |
* If the container format contains both still images and image sequences, |
| 151 |
* SkCodec should choose one of the image sequences for animation. |
| 152 |
*/ |
| 153 |
kPreferAnimation, |
| 154 |
}; |
| 155 |
|
| 156 |
/** |
| 157 |
* If this stream represents an encoded image that we know how to decode, |
| 158 |
* return an SkCodec that can decode it. Otherwise return NULL. |
| 159 |
* |
| 160 |
* As stated above, this call must be able to peek or read |
| 161 |
* MinBufferedBytesNeeded to determine the correct format, and then start |
| 162 |
* reading from the beginning. First it will attempt to peek, and it |
| 163 |
* assumes that if less than MinBufferedBytesNeeded bytes (but more than |
| 164 |
* zero) are returned, this is because the stream is shorter than this, |
| 165 |
* so falling back to reading would not provide more data. If peek() |
| 166 |
* returns zero bytes, this call will instead attempt to read(). This |
| 167 |
* will require that the stream can be rewind()ed. |
| 168 |
* |
| 169 |
* If Result is not NULL, it will be set to either kSuccess if an SkCodec |
| 170 |
* is returned or a reason for the failure if NULL is returned. |
| 171 |
* |
| 172 |
* If SkPngChunkReader is not NULL, take a ref and pass it to libpng if |
| 173 |
* the image is a png. |
| 174 |
* |
| 175 |
* If the SkPngChunkReader is not NULL then: |
| 176 |
* If the image is not a PNG, the SkPngChunkReader will be ignored. |
| 177 |
* If the image is a PNG, the SkPngChunkReader will be reffed. |
| 178 |
* If the PNG has unknown chunks, the SkPngChunkReader will be used |
| 179 |
* to handle these chunks. SkPngChunkReader will be called to read |
| 180 |
* any unknown chunk at any point during the creation of the codec |
| 181 |
* or the decode. Note that if SkPngChunkReader fails to read a |
| 182 |
* chunk, this could result in a failure to create the codec or a |
| 183 |
* failure to decode the image. |
| 184 |
* If the PNG does not contain unknown chunks, the SkPngChunkReader |
| 185 |
* will not be used or modified. |
| 186 |
* |
| 187 |
* If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 188 |
* SkCodec takes ownership of it, and will delete it when done with it. |
| 189 |
*/ |
| 190 |
static std::unique_ptr<SkCodec> MakeFromStream( |
| 191 |
std::unique_ptr<SkStream>, |
| 192 |
SkSpan<const SkCodecs::Decoder> decoders, |
| 193 |
Result* = nullptr, |
| 194 |
SkPngChunkReader* = nullptr, |
| 195 |
SelectionPolicy selectionPolicy = SelectionPolicy::kPreferStillImage); |
| 196 |
// deprecated |
| 197 |
static std::unique_ptr<SkCodec> MakeFromStream( |
| 198 |
std::unique_ptr<SkStream>, |
| 199 |
Result* = nullptr, |
| 200 |
SkPngChunkReader* = nullptr, |
| 201 |
SelectionPolicy selectionPolicy = SelectionPolicy::kPreferStillImage); |
| 202 |
|
| 203 |
/** |
| 204 |
* If this data represents an encoded image that we know how to decode, |
| 205 |
* return an SkCodec that can decode it. Otherwise return NULL. |
| 206 |
* |
| 207 |
* If the SkPngChunkReader is not NULL then: |
| 208 |
* If the image is not a PNG, the SkPngChunkReader will be ignored. |
| 209 |
* If the image is a PNG, the SkPngChunkReader will be reffed. |
| 210 |
* If the PNG has unknown chunks, the SkPngChunkReader will be used |
| 211 |
* to handle these chunks. SkPngChunkReader will be called to read |
| 212 |
* any unknown chunk at any point during the creation of the codec |
| 213 |
* or the decode. Note that if SkPngChunkReader fails to read a |
| 214 |
* chunk, this could result in a failure to create the codec or a |
| 215 |
* failure to decode the image. |
| 216 |
* If the PNG does not contain unknown chunks, the SkPngChunkReader |
| 217 |
* will not be used or modified. |
| 218 |
*/ |
| 219 |
static std::unique_ptr<SkCodec> MakeFromData(sk_sp<const SkData>, |
| 220 |
SkSpan<const SkCodecs::Decoder> decoders, |
| 221 |
SkPngChunkReader* = nullptr); |
| 222 |
// deprecated |
| 223 |
static std::unique_ptr<SkCodec> MakeFromData(sk_sp<const SkData>, SkPngChunkReader* = nullptr); |
| 224 |
|
| 225 |
virtual ~SkCodec(); |
| 226 |
|
| 227 |
/** |
| 228 |
* Return a reasonable SkImageInfo to decode into. |
| 229 |
* |
| 230 |
* If the image has an ICC profile that does not map to an SkColorSpace, |
| 231 |
* the returned SkImageInfo will use SRGB. |
| 232 |
*/ |
| 233 |
SkImageInfo getInfo() const { return fEncodedInfo.makeImageInfo(); } |
| 234 |
|
| 235 |
SkISize dimensions() const { return {fEncodedInfo.width(), fEncodedInfo.height()}; } |
| 236 |
SkIRect bounds() const { |
| 237 |
return SkIRect::MakeWH(fEncodedInfo.width(), fEncodedInfo.height()); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Return the ICC profile of the encoded data. |
| 242 |
*/ |
| 243 |
const skcms_ICCProfile* getICCProfile() const { |
| 244 |
return this->getEncodedInfo().profile(); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Return the HDR metadata associated with this image. Note that even SDR images can include |
| 249 |
* HDR metadata (e.g, indicating how to inverse tone map when displayed on an HDR display). |
| 250 |
*/ |
| 251 |
const skhdr::Metadata& getHdrMetadata() const { return fEncodedInfo.getHdrMetadata(); } |
| 252 |
|
| 253 |
/** |
| 254 |
* Whether the encoded input uses 16 or more bits per component. |
| 255 |
*/ |
| 256 |
bool hasHighBitDepthEncodedData() const { |
| 257 |
// API design note: We don't return `bitsPerComponent` because it may be |
| 258 |
// misleading in some cases - see https://crbug.com/359350061#comment4 |
| 259 |
// for more details. |
| 260 |
return this->getEncodedInfo().bitsPerComponent() >= 16; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the image orientation stored in the EXIF data. |
| 265 |
* If there is no EXIF data, or if we cannot read the EXIF data, returns kTopLeft. |
| 266 |
*/ |
| 267 |
SkEncodedOrigin getOrigin() const { return fOrigin; } |
| 268 |
|
| 269 |
/** |
| 270 |
* Return a size that approximately supports the desired scale factor. |
| 271 |
* The codec may not be able to scale efficiently to the exact scale |
| 272 |
* factor requested, so return a size that approximates that scale. |
| 273 |
* The returned value is the codec's suggestion for the closest valid |
| 274 |
* scale that it can natively support |
| 275 |
*/ |
| 276 |
SkISize getScaledDimensions(float desiredScale) const { |
| 277 |
// Negative and zero scales are errors. |
| 278 |
SkASSERT(desiredScale > 0.0f); |
| 279 |
if (desiredScale <= 0.0f) { |
| 280 |
return SkISize::Make(0, 0); |
| 281 |
} |
| 282 |
|
| 283 |
// Upscaling is not supported. Return the original size if the client |
| 284 |
// requests an upscale. |
| 285 |
if (desiredScale >= 1.0f) { |
| 286 |
return this->dimensions(); |
| 287 |
} |
| 288 |
return this->onGetScaledDimensions(desiredScale); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Return (via desiredSubset) a subset which can decoded from this codec, |
| 293 |
* or false if this codec cannot decode subsets or anything similar to |
| 294 |
* desiredSubset. |
| 295 |
* |
| 296 |
* @param desiredSubset In/out parameter. As input, a desired subset of |
| 297 |
* the original bounds (as specified by getInfo). If true is returned, |
| 298 |
* desiredSubset may have been modified to a subset which is |
| 299 |
* supported. Although a particular change may have been made to |
| 300 |
* desiredSubset to create something supported, it is possible other |
| 301 |
* changes could result in a valid subset. |
| 302 |
* If false is returned, desiredSubset's value is undefined. |
| 303 |
* @return true if this codec supports decoding desiredSubset (as |
| 304 |
* returned, potentially modified) |
| 305 |
*/ |
| 306 |
bool getValidSubset(SkIRect* desiredSubset) const { |
| 307 |
return this->onGetValidSubset(desiredSubset); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Format of the encoded data. |
| 312 |
*/ |
| 313 |
SkEncodedImageFormat getEncodedFormat() const { return this->onGetEncodedFormat(); } |
| 314 |
|
| 315 |
/** |
| 316 |
* Return the underlying encoded data stream. This may be nullptr if the original |
| 317 |
* stream could not be duplicated. |
| 318 |
*/ |
| 319 |
virtual std::unique_ptr<SkStream> getEncodedData() const; |
| 320 |
|
| 321 |
/** |
| 322 |
* Whether or not the memory passed to getPixels is zero initialized. |
| 323 |
*/ |
| 324 |
enum ZeroInitialized { |
| 325 |
/** |
| 326 |
* The memory passed to getPixels is zero initialized. The SkCodec |
| 327 |
* may take advantage of this by skipping writing zeroes. |
| 328 |
*/ |
| 329 |
kYes_ZeroInitialized, |
| 330 |
/** |
| 331 |
* The memory passed to getPixels has not been initialized to zero, |
| 332 |
* so the SkCodec must write all zeroes to memory. |
| 333 |
* |
| 334 |
* This is the default. It will be used if no Options struct is used. |
| 335 |
*/ |
| 336 |
kNo_ZeroInitialized, |
| 337 |
}; |
| 338 |
|
| 339 |
/** |
| 340 |
* Additional options to pass to getPixels. |
| 341 |
*/ |
| 342 |
struct Options { |
| 343 |
Options() |
| 344 |
: fZeroInitialized(kNo_ZeroInitialized) |
| 345 |
, fSubset(nullptr) |
| 346 |
, fFrameIndex(0) |
| 347 |
, fPriorFrame(kNoFrame) |
| 348 |
, fMaxDecodeMemory(0) {} |
| 349 |
|
| 350 |
ZeroInitialized fZeroInitialized; |
| 351 |
/** |
| 352 |
* If not NULL, represents a subset of the original image to decode. |
| 353 |
* Must be within the bounds returned by getInfo(). |
| 354 |
* If the EncodedFormat is SkEncodedImageFormat::kWEBP (the only one which |
| 355 |
* currently supports subsets), the top and left values must be even. |
| 356 |
* |
| 357 |
* In getPixels and incremental decode, we will attempt to decode the |
| 358 |
* exact rectangular subset specified by fSubset. |
| 359 |
* |
| 360 |
* In a scanline decode, it does not make sense to specify a subset |
| 361 |
* top or subset height, since the client already controls which rows |
| 362 |
* to get and which rows to skip. During scanline decodes, we will |
| 363 |
* require that the subset top be zero and the subset height be equal |
| 364 |
* to the full height. We will, however, use the values of |
| 365 |
* subset left and subset width to decode partial scanlines on calls |
| 366 |
* to getScanlines(). |
| 367 |
*/ |
| 368 |
const SkIRect* fSubset; |
| 369 |
|
| 370 |
/** |
| 371 |
* The frame to decode. |
| 372 |
* |
| 373 |
* Only meaningful for multi-frame images. |
| 374 |
*/ |
| 375 |
int fFrameIndex; |
| 376 |
|
| 377 |
/** |
| 378 |
* If not kNoFrame, the dst already contains the prior frame at this index. |
| 379 |
* |
| 380 |
* Only meaningful for multi-frame images. |
| 381 |
* |
| 382 |
* If fFrameIndex needs to be blended with a prior frame (as reported by |
| 383 |
* getFrameInfo[fFrameIndex].fRequiredFrame), the client can set this to |
| 384 |
* any non-kRestorePrevious frame in [fRequiredFrame, fFrameIndex) to |
| 385 |
* indicate that that frame is already in the dst. Options.fZeroInitialized |
| 386 |
* is ignored in this case. |
| 387 |
* |
| 388 |
* If set to kNoFrame, the codec will decode any necessary required frame(s) first. |
| 389 |
*/ |
| 390 |
int fPriorFrame; |
| 391 |
|
| 392 |
/** |
| 393 |
* If non-zero, image decoding will fail if cumulative allocations exceed this many bytes. |
| 394 |
*/ |
| 395 |
size_t fMaxDecodeMemory; |
| 396 |
}; |
| 397 |
|
| 398 |
/** |
| 399 |
* Decode into the given pixels, a block of memory of size at |
| 400 |
* least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 401 |
* bytesPerPixel) |
| 402 |
* |
| 403 |
* Repeated calls to this function should give the same results, |
| 404 |
* allowing the PixelRef to be immutable. |
| 405 |
* |
| 406 |
* @param info A description of the format (config, size) |
| 407 |
* expected by the caller. This can simply be identical |
| 408 |
* to the info returned by getInfo(). |
| 409 |
* |
| 410 |
* This contract also allows the caller to specify |
| 411 |
* different output-configs, which the implementation can |
| 412 |
* decide to support or not. |
| 413 |
* |
| 414 |
* A size that does not match getInfo() implies a request |
| 415 |
* to scale. If the generator cannot perform this scale, |
| 416 |
* it will return kInvalidScale. |
| 417 |
* |
| 418 |
* If the info contains a non-null SkColorSpace, the codec |
| 419 |
* will perform the appropriate color space transformation. |
| 420 |
* |
| 421 |
* If the caller passes in the SkColorSpace that maps to the |
| 422 |
* ICC profile reported by getICCProfile(), the color space |
| 423 |
* transformation is a no-op. |
| 424 |
* |
| 425 |
* If the caller passes a null SkColorSpace, no color space |
| 426 |
* transformation will be done. |
| 427 |
* |
| 428 |
* If a scanline decode is in progress, scanline mode will end, requiring the client to call |
| 429 |
* startScanlineDecode() in order to return to decoding scanlines. |
| 430 |
* |
| 431 |
* For certain codecs, reading into a smaller bitmap than the original dimensions may not |
| 432 |
* produce correct results (e.g. animated webp). |
| 433 |
* |
| 434 |
* @return Result kSuccess, or another value explaining the type of failure. |
| 435 |
*/ |
| 436 |
Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options*); |
| 437 |
|
| 438 |
/** |
| 439 |
* Simplified version of getPixels() that uses the default Options. |
| 440 |
*/ |
| 441 |
Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 442 |
return this->getPixels(info, pixels, rowBytes, nullptr); |
| 443 |
} |
| 444 |
|
| 445 |
Result getPixels(const SkPixmap& pm, const Options* opts = nullptr) { |
| 446 |
return this->getPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), opts); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Return an image containing the pixels. If the codec's origin is not "upper left", |
| 451 |
* This will rotate the output image accordingly. |
| 452 |
*/ |
| 453 |
std::tuple<sk_sp<SkImage>, SkCodec::Result> getImage(const SkImageInfo& info, |
| 454 |
const Options* opts = nullptr); |
| 455 |
std::tuple<sk_sp<SkImage>, SkCodec::Result> getImage(); |
| 456 |
|
| 457 |
/** |
| 458 |
* If decoding to YUV is supported, this returns true. Otherwise, this |
| 459 |
* returns false and the caller will ignore output parameter yuvaPixmapInfo. |
| 460 |
* |
| 461 |
* @param supportedDataTypes Indicates the data type/planar config combinations that are |
| 462 |
* supported by the caller. If the generator supports decoding to |
| 463 |
* YUV(A), but not as a type in supportedDataTypes, this method |
| 464 |
* returns false. |
| 465 |
* @param yuvaPixmapInfo Output parameter that specifies the planar configuration, subsampling, |
| 466 |
* orientation, chroma siting, plane color types, and row bytes. |
| 467 |
*/ |
| 468 |
bool queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes, |
| 469 |
SkYUVAPixmapInfo* yuvaPixmapInfo) const; |
| 470 |
|
| 471 |
/** |
| 472 |
* Returns kSuccess, or another value explaining the type of failure. |
| 473 |
* This always attempts to perform a full decode. To get the planar |
| 474 |
* configuration without decoding use queryYUVAInfo(). |
| 475 |
* |
| 476 |
* @param yuvaPixmaps Contains preallocated pixmaps configured according to a successful call |
| 477 |
* to queryYUVAInfo(). |
| 478 |
*/ |
| 479 |
Result getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps); |
| 480 |
|
| 481 |
/** |
| 482 |
* Prepare for an incremental decode with the specified options. |
| 483 |
* |
| 484 |
* This may require a rewind. |
| 485 |
* |
| 486 |
* If kIncompleteInput is returned, may be called again after more data has |
| 487 |
* been provided to the source SkStream. |
| 488 |
* |
| 489 |
* @param dstInfo Info of the destination. If the dimensions do not match |
| 490 |
* those of getInfo, this implies a scale. |
| 491 |
* @param dst Memory to write to. Needs to be large enough to hold the subset, |
| 492 |
* if present, or the full image as described in dstInfo. |
| 493 |
* @param options Contains decoding options, including if memory is zero |
| 494 |
* initialized and whether to decode a subset. |
| 495 |
* @return Enum representing success or reason for failure. |
| 496 |
*/ |
| 497 |
Result startIncrementalDecode(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
| 498 |
const Options*); |
| 499 |
|
| 500 |
Result startIncrementalDecode(const SkImageInfo& dstInfo, void* dst, size_t rowBytes) { |
| 501 |
return this->startIncrementalDecode(dstInfo, dst, rowBytes, nullptr); |
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* Start/continue the incremental decode. |
| 506 |
* |
| 507 |
* Not valid to call before a call to startIncrementalDecode() returns |
| 508 |
* kSuccess. |
| 509 |
* |
| 510 |
* If kIncompleteInput is returned, may be called again after more data has |
| 511 |
* been provided to the source SkStream. |
| 512 |
* |
| 513 |
* Unlike getPixels and getScanlines, this does not do any filling. This is |
| 514 |
* left up to the caller, since they may be skipping lines or continuing the |
| 515 |
* decode later. In the latter case, they may choose to initialize all lines |
| 516 |
* first, or only initialize the remaining lines after the first call. |
| 517 |
* |
| 518 |
* @param rowsDecoded Optional output variable returning the total number of |
| 519 |
* lines initialized. Only meaningful if this method returns kIncompleteInput. |
| 520 |
* Otherwise the implementation may not set it. |
| 521 |
* Note that some implementations may have initialized this many rows, but |
| 522 |
* not necessarily finished those rows (e.g. interlaced PNG). This may be |
| 523 |
* useful for determining what rows the client needs to initialize. |
| 524 |
* @return kSuccess if all lines requested in startIncrementalDecode have |
| 525 |
* been completely decoded. kIncompleteInput otherwise. |
| 526 |
*/ |
| 527 |
Result incrementalDecode(int* rowsDecoded = nullptr) { |
| 528 |
if (!fStartedIncrementalDecode) { |
| 529 |
return kInvalidParameters; |
| 530 |
} |
| 531 |
return this->onIncrementalDecode(rowsDecoded); |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* The remaining functions revolve around decoding scanlines. |
| 536 |
*/ |
| 537 |
|
| 538 |
/** |
| 539 |
* Prepare for a scanline decode with the specified options. |
| 540 |
* |
| 541 |
* After this call, this class will be ready to decode the first scanline. |
| 542 |
* |
| 543 |
* This must be called in order to call getScanlines or skipScanlines. |
| 544 |
* |
| 545 |
* This may require rewinding the stream. |
| 546 |
* |
| 547 |
* Not all SkCodecs support this. |
| 548 |
* |
| 549 |
* @param dstInfo Info of the destination. If the dimensions do not match |
| 550 |
* those of getInfo, this implies a scale. |
| 551 |
* @param options Contains decoding options, including if memory is zero |
| 552 |
* initialized. |
| 553 |
* @return Enum representing success or reason for failure. |
| 554 |
*/ |
| 555 |
Result startScanlineDecode(const SkImageInfo& dstInfo, const Options* options); |
| 556 |
|
| 557 |
/** |
| 558 |
* Simplified version of startScanlineDecode() that uses the default Options. |
| 559 |
*/ |
| 560 |
Result startScanlineDecode(const SkImageInfo& dstInfo) { |
| 561 |
return this->startScanlineDecode(dstInfo, nullptr); |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Write the next countLines scanlines into dst. |
| 566 |
* |
| 567 |
* Not valid to call before calling startScanlineDecode(). |
| 568 |
* |
| 569 |
* @param dst Must be non-null, and large enough to hold countLines |
| 570 |
* scanlines of size rowBytes. |
| 571 |
* @param countLines Number of lines to write. |
| 572 |
* @param rowBytes Number of bytes per row. Must be large enough to hold |
| 573 |
* a scanline based on the SkImageInfo used to create this object. |
| 574 |
* @return the number of lines successfully decoded. If this value is |
| 575 |
* less than countLines, this will fill the remaining lines with a |
| 576 |
* default value. |
| 577 |
*/ |
| 578 |
int getScanlines(void* dst, int countLines, size_t rowBytes); |
| 579 |
|
| 580 |
/** |
| 581 |
* Skip count scanlines. |
| 582 |
* |
| 583 |
* Not valid to call before calling startScanlineDecode(). |
| 584 |
* |
| 585 |
* The default version just calls onGetScanlines and discards the dst. |
| 586 |
* NOTE: If skipped lines are the only lines with alpha, this default |
| 587 |
* will make reallyHasAlpha return true, when it could have returned |
| 588 |
* false. |
| 589 |
* |
| 590 |
* @return true if the scanlines were successfully skipped |
| 591 |
* false on failure, possible reasons for failure include: |
| 592 |
* An incomplete input image stream. |
| 593 |
* Calling this function before calling startScanlineDecode(). |
| 594 |
* If countLines is less than zero or so large that it moves |
| 595 |
* the current scanline past the end of the image. |
| 596 |
*/ |
| 597 |
bool skipScanlines(int countLines); |
| 598 |
|
| 599 |
/** |
| 600 |
* The order in which rows are output from the scanline decoder is not the |
| 601 |
* same for all variations of all image types. This explains the possible |
| 602 |
* output row orderings. |
| 603 |
*/ |
| 604 |
enum SkScanlineOrder { |
| 605 |
/* |
| 606 |
* By far the most common, this indicates that the image can be decoded |
| 607 |
* reliably using the scanline decoder, and that rows will be output in |
| 608 |
* the logical order. |
| 609 |
*/ |
| 610 |
kTopDown_SkScanlineOrder, |
| 611 |
|
| 612 |
/* |
| 613 |
* This indicates that the scanline decoder reliably outputs rows, but |
| 614 |
* they will be returned in reverse order. If the scanline format is |
| 615 |
* kBottomUp, the nextScanline() API can be used to determine the actual |
| 616 |
* y-coordinate of the next output row, but the client is not forced |
| 617 |
* to take advantage of this, given that it's not too tough to keep |
| 618 |
* track independently. |
| 619 |
* |
| 620 |
* For full image decodes, it is safe to get all of the scanlines at |
| 621 |
* once, since the decoder will handle inverting the rows as it |
| 622 |
* decodes. |
| 623 |
* |
| 624 |
* For subset decodes and sampling, it is simplest to get and skip |
| 625 |
* scanlines one at a time, using the nextScanline() API. It is |
| 626 |
* possible to ask for larger chunks at a time, but this should be used |
| 627 |
* with caution. As with full image decodes, the decoder will handle |
| 628 |
* inverting the requested rows, but rows will still be delivered |
| 629 |
* starting from the bottom of the image. |
| 630 |
* |
| 631 |
* Upside down bmps are an example. |
| 632 |
*/ |
| 633 |
kBottomUp_SkScanlineOrder, |
| 634 |
}; |
| 635 |
|
| 636 |
/** |
| 637 |
* An enum representing the order in which scanlines will be returned by |
| 638 |
* the scanline decoder. |
| 639 |
* |
| 640 |
* This is undefined before startScanlineDecode() is called. |
| 641 |
*/ |
| 642 |
SkScanlineOrder getScanlineOrder() const { return this->onGetScanlineOrder(); } |
| 643 |
|
| 644 |
/** |
| 645 |
* Returns the y-coordinate of the next row to be returned by the scanline |
| 646 |
* decoder. |
| 647 |
* |
| 648 |
* This will equal fCurrScanline, except in the case of strangely |
| 649 |
* encoded image types (bottom-up bmps). |
| 650 |
* |
| 651 |
* Results are undefined when not in scanline decoding mode. |
| 652 |
*/ |
| 653 |
int nextScanline() const { return this->outputScanline(fCurrScanline); } |
| 654 |
|
| 655 |
/** |
| 656 |
* Returns the output y-coordinate of the row that corresponds to an input |
| 657 |
* y-coordinate. The input y-coordinate represents where the scanline |
| 658 |
* is located in the encoded data. |
| 659 |
* |
| 660 |
* This will equal inputScanline, except in the case of strangely |
| 661 |
* encoded image types (bottom-up bmps, interlaced gifs). |
| 662 |
*/ |
| 663 |
int outputScanline(int inputScanline) const; |
| 664 |
|
| 665 |
/** |
| 666 |
* Return the number of frames in the image. |
| 667 |
* |
| 668 |
* May require reading through the stream. |
| 669 |
* |
| 670 |
* Note that some codecs may be unable to gather `FrameInfo` for all frames |
| 671 |
* in case of `kIncompleteInput`. For such codecs `getFrameCount` may |
| 672 |
* initially report a low frame count. After the underlying `SkStream` |
| 673 |
* provides additional data, then calling `getFrameCount` again may return |
| 674 |
* an updated, increased frame count. |
| 675 |
*/ |
| 676 |
int getFrameCount() { |
| 677 |
return this->onGetFrameCount(); |
| 678 |
} |
| 679 |
|
| 680 |
// Sentinel value used when a frame index implies "no frame": |
| 681 |
// - FrameInfo::fRequiredFrame set to this value means the frame |
| 682 |
// is independent. |
| 683 |
// - Options::fPriorFrame set to this value means no (relevant) prior frame |
| 684 |
// is residing in dst's memory. |
| 685 |
static constexpr int kNoFrame = -1; |
| 686 |
|
| 687 |
/** |
| 688 |
* Information about individual frames in a multi-framed image. |
| 689 |
*/ |
| 690 |
struct FrameInfo { |
| 691 |
/** |
| 692 |
* The frame that this frame needs to be blended with, or |
| 693 |
* kNoFrame if this frame is independent (so it can be |
| 694 |
* drawn over an uninitialized buffer). |
| 695 |
* |
| 696 |
* Note that this is the *earliest* frame that can be used |
| 697 |
* for blending. Any frame from [fRequiredFrame, i) can be |
| 698 |
* used, unless its fDisposalMethod is kRestorePrevious. |
| 699 |
*/ |
| 700 |
int fRequiredFrame; |
| 701 |
|
| 702 |
/** |
| 703 |
* Number of milliseconds to show this frame. |
| 704 |
*/ |
| 705 |
int fDuration; |
| 706 |
|
| 707 |
/** |
| 708 |
* Whether the end marker for this frame is contained in the stream. |
| 709 |
* |
| 710 |
* Note: this does not guarantee that an attempt to decode will be complete. |
| 711 |
* There could be an error in the stream. |
| 712 |
*/ |
| 713 |
bool fFullyReceived; |
| 714 |
|
| 715 |
/** |
| 716 |
* This is conservative; it will still return non-opaque if e.g. a |
| 717 |
* color index-based frame has a color with alpha but does not use it. |
| 718 |
*/ |
| 719 |
SkAlphaType fAlphaType; |
| 720 |
|
| 721 |
/** |
| 722 |
* Whether the updated rectangle contains alpha. |
| 723 |
* |
| 724 |
* This is conservative; it will still be set to true if e.g. a color |
| 725 |
* index-based frame has a color with alpha but does not use it. In |
| 726 |
* addition, it may be set to true, even if the final frame, after |
| 727 |
* blending, is opaque. |
| 728 |
*/ |
| 729 |
bool fHasAlphaWithinBounds; |
| 730 |
|
| 731 |
/** |
| 732 |
* How this frame should be modified before decoding the next one. |
| 733 |
*/ |
| 734 |
SkCodecAnimation::DisposalMethod fDisposalMethod; |
| 735 |
|
| 736 |
/** |
| 737 |
* How this frame should blend with the prior frame. |
| 738 |
*/ |
| 739 |
SkCodecAnimation::Blend fBlend; |
| 740 |
|
| 741 |
/** |
| 742 |
* The rectangle updated by this frame. |
| 743 |
* |
| 744 |
* It may be empty, if the frame does not change the image. It will |
| 745 |
* always be contained by SkCodec::dimensions(). |
| 746 |
*/ |
| 747 |
SkIRect fFrameRect; |
| 748 |
}; |
| 749 |
|
| 750 |
/** |
| 751 |
* Return info about a single frame. |
| 752 |
* |
| 753 |
* Does not read through the stream, so it should be called after |
| 754 |
* getFrameCount() to parse any frames that have not already been parsed. |
| 755 |
* |
| 756 |
* Only supported by animated (multi-frame) codecs. Note that this is a |
| 757 |
* property of the codec (the SkCodec subclass), not the image. |
| 758 |
* |
| 759 |
* To elaborate, some codecs support animation (e.g. GIF). Others do not |
| 760 |
* (e.g. BMP). Animated codecs can still represent single frame images. |
| 761 |
* Calling getFrameInfo(0, etc) will return true for a single frame GIF |
| 762 |
* even if the overall image is not animated (in that the pixels on screen |
| 763 |
* do not change over time). When incrementally decoding a GIF image, we |
| 764 |
* might only know that there's a single frame *so far*. |
| 765 |
* |
| 766 |
* For non-animated SkCodec subclasses, it's sufficient but not necessary |
| 767 |
* for this method to always return false. |
| 768 |
*/ |
| 769 |
bool getFrameInfo(int index, FrameInfo* info) const { |
| 770 |
if (index < 0) { |
| 771 |
return false; |
| 772 |
} |
| 773 |
return this->onGetFrameInfo(index, info); |
| 774 |
} |
| 775 |
|
| 776 |
/** |
| 777 |
* Return info about all the frames in the image. |
| 778 |
* |
| 779 |
* May require reading through the stream to determine info about the |
| 780 |
* frames (including the count). |
| 781 |
* |
| 782 |
* As such, future decoding calls may require a rewind. |
| 783 |
* |
| 784 |
* This may return an empty vector for non-animated codecs. See the |
| 785 |
* getFrameInfo(int, FrameInfo*) comment. |
| 786 |
*/ |
| 787 |
std::vector<FrameInfo> getFrameInfo(); |
| 788 |
|
| 789 |
static constexpr int kRepetitionCountInfinite = -1; |
| 790 |
|
| 791 |
/** |
| 792 |
* Return the number of times to repeat, if this image is animated. This number does not |
| 793 |
* include the first play through of each frame. For example, a repetition count of 4 means |
| 794 |
* that each frame is played 5 times and then the animation stops. |
| 795 |
* |
| 796 |
* It can return kRepetitionCountInfinite, a negative number, meaning that the animation |
| 797 |
* should loop forever. |
| 798 |
* |
| 799 |
* May require reading the stream to find the repetition count. |
| 800 |
* |
| 801 |
* As such, future decoding calls may require a rewind. |
| 802 |
* |
| 803 |
* `getRepetitionCount` will return `0` in two cases: |
| 804 |
* 1. Still (non-animated) images. |
| 805 |
* 2. Animated images that only play the animation once (i.e. that don't |
| 806 |
* repeat the animation) |
| 807 |
* `isAnimated` can be used to disambiguate between these two cases. |
| 808 |
*/ |
| 809 |
int getRepetitionCount() { |
| 810 |
return this->onGetRepetitionCount(); |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* `isAnimated` returns whether the full input is expected to contain an |
| 815 |
* animated image (i.e. more than 1 image frame). This can be used to |
| 816 |
* disambiguate the meaning of `getRepetitionCount` returning `0` (see |
| 817 |
* `getRepetitionCount`'s doc comment for more details). |
| 818 |
* |
| 819 |
* Note that in some codecs `getFrameCount()` only returns the number of |
| 820 |
* frames for which all the metadata has been already successfully decoded. |
| 821 |
* Therefore for a partial input `isAnimated()` may return "yes", even |
| 822 |
* though `getFrameCount()` may temporarily return `1` until more of the |
| 823 |
* input is available. |
| 824 |
* |
| 825 |
* When handling partial input, some codecs may not know until later (e.g. |
| 826 |
* until encountering additional image frames) whether the given image has |
| 827 |
* more than one frame. Such codecs may initially return |
| 828 |
* `IsAnimated::kUnknown` and only later give a definitive "yes" or "no" |
| 829 |
* answer. GIF format is one example where this may happen. |
| 830 |
* |
| 831 |
* Other codecs may be able to decode the information from the metadata |
| 832 |
* present before the first image frame. Such codecs should be able to give |
| 833 |
* a definitive "yes" or "no" answer as soon as they are constructed. PNG |
| 834 |
* format is one example where this happens. |
| 835 |
*/ |
| 836 |
enum class IsAnimated { |
| 837 |
kYes, |
| 838 |
kNo, |
| 839 |
kUnknown, |
| 840 |
}; |
| 841 |
IsAnimated isAnimated() { return this->onIsAnimated(); } |
| 842 |
|
| 843 |
// Register a decoder at runtime by passing two function pointers: |
| 844 |
// - peek() to return true if the span of bytes appears to be your encoded format; |
| 845 |
// - make() to attempt to create an SkCodec from the given stream. |
| 846 |
// Not thread safe. |
| 847 |
static void Register( |
| 848 |
bool (*peek)(const void*, size_t), |
| 849 |
std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)); |
| 850 |
|
| 851 |
protected: |
| 852 |
const SkEncodedInfo& getEncodedInfo() const { return fEncodedInfo; } |
| 853 |
|
| 854 |
using XformFormat = skcms_PixelFormat; |
| 855 |
|
| 856 |
SkCodec(SkEncodedInfo&&, |
| 857 |
XformFormat srcFormat, |
| 858 |
std::unique_ptr<SkStream>, |
| 859 |
SkEncodedOrigin = kTopLeft_SkEncodedOrigin); |
| 860 |
|
| 861 |
virtual bool onGetGainmapCodec(SkGainmapInfo*, std::unique_ptr<SkCodec>*) { return false; } |
| 862 |
virtual bool onGetGainmapInfo(SkGainmapInfo*) { return false; } |
| 863 |
|
| 864 |
// TODO(issues.skia.org/363544350): This API only works for JPEG images. Remove this API once |
| 865 |
// it is no longer used. |
| 866 |
virtual bool onGetGainmapInfo(SkGainmapInfo*, std::unique_ptr<SkStream>*) { return false; } |
| 867 |
|
| 868 |
virtual SkISize onGetScaledDimensions(float /*desiredScale*/) const { |
| 869 |
// By default, scaling is not supported. |
| 870 |
return this->dimensions(); |
| 871 |
} |
| 872 |
|
| 873 |
// FIXME: What to do about subsets?? |
| 874 |
/** |
| 875 |
* Subclasses should override if they support dimensions other than the |
| 876 |
* srcInfo's. |
| 877 |
*/ |
| 878 |
virtual bool onDimensionsSupported(const SkISize&) { |
| 879 |
return false; |
| 880 |
} |
| 881 |
|
| 882 |
virtual SkEncodedImageFormat onGetEncodedFormat() const = 0; |
| 883 |
|
| 884 |
/** |
| 885 |
* @param rowsDecoded When the encoded image stream is incomplete, this function |
| 886 |
* will return kIncompleteInput and rowsDecoded will be set to |
| 887 |
* the number of scanlines that were successfully decoded. |
| 888 |
* This will allow getPixels() to fill the uninitialized memory. |
| 889 |
*/ |
| 890 |
virtual Result onGetPixels(const SkImageInfo& info, |
| 891 |
void* pixels, size_t rowBytes, const Options&, |
| 892 |
int* rowsDecoded) = 0; |
| 893 |
|
| 894 |
virtual bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&, |
| 895 |
SkYUVAPixmapInfo*) const { return false; } |
| 896 |
|
| 897 |
virtual Result onGetYUVAPlanes(const SkYUVAPixmaps&) { return kUnimplemented; } |
| 898 |
|
| 899 |
virtual bool onGetValidSubset(SkIRect* /*desiredSubset*/) const { |
| 900 |
// By default, subsets are not supported. |
| 901 |
return false; |
| 902 |
} |
| 903 |
|
| 904 |
/** |
| 905 |
* If the stream was previously read, attempt to rewind. |
| 906 |
* |
| 907 |
* If the stream needed to be rewound, call onRewind. |
| 908 |
* @returns true if the codec is at the right position and can be used. |
| 909 |
* false if there was a failure to rewind. |
| 910 |
* |
| 911 |
* This is called by getPixels(), getYUV8Planes(), startIncrementalDecode() and |
| 912 |
* startScanlineDecode(). Subclasses may call if they need to rewind at another time. |
| 913 |
*/ |
| 914 |
[[nodiscard]] bool rewindIfNeeded(); |
| 915 |
|
| 916 |
/** |
| 917 |
* Called by onRewind(), attempts to rewind fStream. |
| 918 |
*/ |
| 919 |
bool rewindStream(); |
| 920 |
|
| 921 |
/** |
| 922 |
* Called by rewindIfNeeded, if the stream needed to be rewound. |
| 923 |
* |
| 924 |
* Subclasses should call rewindStream() if they own one, and then |
| 925 |
* do any set up needed after. |
| 926 |
*/ |
| 927 |
virtual bool onRewind() { |
| 928 |
if (!this->rewindStream()) { |
| 929 |
return false; |
| 930 |
} |
| 931 |
return true; |
| 932 |
} |
| 933 |
|
| 934 |
/** |
| 935 |
* Get method for the input stream |
| 936 |
*/ |
| 937 |
SkStream* stream() { |
| 938 |
return fStream.get(); |
| 939 |
} |
| 940 |
|
| 941 |
/** |
| 942 |
* The remaining functions revolve around decoding scanlines. |
| 943 |
*/ |
| 944 |
|
| 945 |
/** |
| 946 |
* Most images types will be kTopDown and will not need to override this function. |
| 947 |
*/ |
| 948 |
virtual SkScanlineOrder onGetScanlineOrder() const { return kTopDown_SkScanlineOrder; } |
| 949 |
|
| 950 |
const SkImageInfo& dstInfo() const { return fDstInfo; } |
| 951 |
|
| 952 |
const Options& options() const { return fOptions; } |
| 953 |
|
| 954 |
/** |
| 955 |
* Returns the number of scanlines that have been decoded so far. |
| 956 |
* This is unaffected by the SkScanlineOrder. |
| 957 |
* |
| 958 |
* Returns -1 if we have not started a scanline decode. |
| 959 |
*/ |
| 960 |
int currScanline() const { return fCurrScanline; } |
| 961 |
|
| 962 |
virtual int onOutputScanline(int inputScanline) const; |
| 963 |
|
| 964 |
/** |
| 965 |
* Return whether we can convert to dst. |
| 966 |
* |
| 967 |
* Will be called for the appropriate frame, prior to initializing the colorXform. |
| 968 |
*/ |
| 969 |
virtual bool conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, |
| 970 |
bool needsColorXform); |
| 971 |
|
| 972 |
// Some classes never need a colorXform e.g. |
| 973 |
// - ICO uses its embedded codec's colorXform |
| 974 |
// - WBMP is just Black/White |
| 975 |
virtual bool usesColorXform() const { return true; } |
| 976 |
void applyColorXform(void* dst, const void* src, int count) const; |
| 977 |
|
| 978 |
bool colorXform() const { return fXformTime != kNo_XformTime; } |
| 979 |
bool xformOnDecode() const { return fXformTime == kDecodeRow_XformTime; } |
| 980 |
|
| 981 |
virtual int onGetFrameCount() { |
| 982 |
return 1; |
| 983 |
} |
| 984 |
|
| 985 |
virtual bool onGetFrameInfo(int, FrameInfo*) const { |
| 986 |
return false; |
| 987 |
} |
| 988 |
|
| 989 |
virtual int onGetRepetitionCount() { |
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
virtual IsAnimated onIsAnimated() { |
| 994 |
return IsAnimated::kNo; |
| 995 |
} |
| 996 |
|
| 997 |
// Returns true if the requested amount keeps the current total under Options::fMaxDecodeMemory. |
| 998 |
bool allocateFromBudget(size_t numBytes); |
| 999 |
|
| 1000 |
private: |
| 1001 |
const SkEncodedInfo fEncodedInfo; |
| 1002 |
XformFormat fSrcXformFormat; |
| 1003 |
std::unique_ptr<SkStream> fStream; |
| 1004 |
bool fNeedsRewind = false; |
| 1005 |
const SkEncodedOrigin fOrigin; |
| 1006 |
|
| 1007 |
SkImageInfo fDstInfo; |
| 1008 |
Options fOptions; |
| 1009 |
|
| 1010 |
enum XformTime { |
| 1011 |
kNo_XformTime, |
| 1012 |
kPalette_XformTime, |
| 1013 |
kDecodeRow_XformTime, |
| 1014 |
}; |
| 1015 |
XformTime fXformTime; |
| 1016 |
XformFormat fDstXformFormat; // Based on fDstInfo. |
| 1017 |
skcms_ICCProfile fDstProfileStorage; |
| 1018 |
// This tracks either fDstProfileStorage or the ICC profile in fEncodedInfo. |
| 1019 |
// For the latter case it is important to not make a profile copy because skcms_Transform |
| 1020 |
// only performs a shallow pointer comparison to decide whether it can skip the color space |
| 1021 |
// transformation. |
| 1022 |
const skcms_ICCProfile* fDstProfile = &fDstProfileStorage; |
| 1023 |
skcms_AlphaFormat fDstXformAlphaFormat; |
| 1024 |
|
| 1025 |
// Only meaningful during scanline decodes. |
| 1026 |
int fCurrScanline = -1; |
| 1027 |
|
| 1028 |
// How many bytes we are allowed to use when decoding. |
| 1029 |
size_t fDecodeBudget = 0; |
| 1030 |
|
| 1031 |
bool fStartedIncrementalDecode = false; |
| 1032 |
|
| 1033 |
// Allows SkAndroidCodec to call handleFrameIndex (potentially decoding a prior frame and |
| 1034 |
// clearing to transparent) without SkCodec itself calling it, too. |
| 1035 |
bool fUsingCallbackForHandleFrameIndex = false; |
| 1036 |
|
| 1037 |
bool initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha, bool srcIsOpaque); |
| 1038 |
|
| 1039 |
/** |
| 1040 |
* Return whether these dimensions are supported as a scale. |
| 1041 |
* |
| 1042 |
* The codec may choose to cache the information about scale and subset. |
| 1043 |
* Either way, the same information will be passed to onGetPixels/onStart |
| 1044 |
* on success. |
| 1045 |
* |
| 1046 |
* This must return true for a size returned from getScaledDimensions. |
| 1047 |
*/ |
| 1048 |
bool dimensionsSupported(const SkISize& dim) { |
| 1049 |
return dim == this->dimensions() || this->onDimensionsSupported(dim); |
| 1050 |
} |
| 1051 |
|
| 1052 |
Result getPixelsBudgeted(const SkImageInfo& info, |
| 1053 |
void* pixels, |
| 1054 |
size_t rowBytes, |
| 1055 |
const Options*); |
| 1056 |
|
| 1057 |
/** |
| 1058 |
* For multi-framed images, return the object with information about the frames. |
| 1059 |
*/ |
| 1060 |
virtual const SkFrameHolder* getFrameHolder() const { |
| 1061 |
return nullptr; |
| 1062 |
} |
| 1063 |
|
| 1064 |
// Callback for decoding a prior frame. The `Options::fFrameIndex` is ignored, |
| 1065 |
// being replaced by frameIndex. This allows opts to actually be a subclass of |
| 1066 |
// SkCodec::Options which SkCodec itself does not know how to copy or modify, |
| 1067 |
// but just passes through to the caller (where it can be reinterpret_cast'd). |
| 1068 |
using GetPixelsCallback = std::function<Result(const SkImageInfo&, void* pixels, |
| 1069 |
size_t rowBytes, const Options& opts, |
| 1070 |
int frameIndex)>; |
| 1071 |
|
| 1072 |
/** |
| 1073 |
* Check for a valid Options.fFrameIndex, and decode prior frames if necessary. |
| 1074 |
* |
| 1075 |
* If GetPixelsCallback is not null, it will be used to decode a prior frame instead |
| 1076 |
* of using this SkCodec directly. It may also be used recursively, if that in turn |
| 1077 |
* depends on a prior frame. This is used by SkAndroidCodec. |
| 1078 |
*/ |
| 1079 |
Result handleFrameIndex(const SkImageInfo&, void* pixels, size_t rowBytes, const Options&, |
| 1080 |
GetPixelsCallback = nullptr); |
| 1081 |
|
| 1082 |
// Methods for scanline decoding. |
| 1083 |
virtual Result onStartScanlineDecode(const SkImageInfo& /*dstInfo*/, |
| 1084 |
const Options& /*options*/) { |
| 1085 |
return kUnimplemented; |
| 1086 |
} |
| 1087 |
|
| 1088 |
/** |
| 1089 |
* Checks whether the implementation supports incremental decoding for the given info. |
| 1090 |
* |
| 1091 |
* Note that onStartIncrementalDecode can stil fail regardless of this result. |
| 1092 |
*/ |
| 1093 |
virtual bool onSupportsIncrementalDecode(const SkImageInfo&) { return false; } |
| 1094 |
|
| 1095 |
virtual Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t, |
| 1096 |
const Options&) { |
| 1097 |
return kUnimplemented; |
| 1098 |
} |
| 1099 |
|
| 1100 |
virtual Result onIncrementalDecode(int*) { |
| 1101 |
return kUnimplemented; |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
virtual bool onSkipScanlines(int /*countLines*/) { return false; } |
| 1106 |
|
| 1107 |
virtual int onGetScanlines(void* /*dst*/, int /*countLines*/, size_t /*rowBytes*/) { return 0; } |
| 1108 |
|
| 1109 |
/** |
| 1110 |
* On an incomplete decode, getPixels() and getScanlines() will call this function |
| 1111 |
* to fill any uinitialized memory. |
| 1112 |
* |
| 1113 |
* @param dstInfo Contains the destination color type |
| 1114 |
* Contains the destination alpha type |
| 1115 |
* Contains the destination width |
| 1116 |
* The height stored in this info is unused |
| 1117 |
* @param dst Pointer to the start of destination pixel memory |
| 1118 |
* @param rowBytes Stride length in destination pixel memory |
| 1119 |
* @param zeroInit Indicates if memory is zero initialized |
| 1120 |
* @param linesRequested Number of lines that the client requested |
| 1121 |
* @param linesDecoded Number of lines that were successfully decoded |
| 1122 |
*/ |
| 1123 |
void fillIncompleteImage(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
| 1124 |
ZeroInitialized zeroInit, int linesRequested, int linesDecoded); |
| 1125 |
|
| 1126 |
/** |
| 1127 |
* Return an object which will allow forcing scanline decodes to sample in X. |
| 1128 |
* |
| 1129 |
* May create a sampler, if one is not currently being used. Otherwise, does |
| 1130 |
* not affect ownership. |
| 1131 |
* |
| 1132 |
* Only valid during scanline decoding or incremental decoding. |
| 1133 |
*/ |
| 1134 |
virtual SkSampler* getSampler(bool /*createIfNecessary*/) { return nullptr; } |
| 1135 |
|
| 1136 |
friend class DM::CodecSrc; // for fillIncompleteImage |
| 1137 |
friend class PNGCodecGM; // for fillIncompleteImage |
| 1138 |
friend class SkSampledCodec; |
| 1139 |
friend class SkIcoCodec; |
| 1140 |
friend class SkPngCodec; // for onGetGainmapCodec |
| 1141 |
friend class SkAndroidCodec; // for handleFrameIndex |
| 1142 |
friend class SkCodecPriv; // for fEncodedInfo |
| 1143 |
}; |
| 1144 |
|
| 1145 |
namespace SkCodecs { |
| 1146 |
|
| 1147 |
using DecodeContext = void*; |
| 1148 |
using IsFormatCallback = bool (*)(const void* data, size_t len); |
| 1149 |
using MakeFromStreamCallback = std::unique_ptr<SkCodec> (*)(std::unique_ptr<SkStream>, |
| 1150 |
SkCodec::Result*, |
| 1151 |
DecodeContext); |
| 1152 |
|
| 1153 |
struct SK_API Decoder { |
| 1154 |
// By convention, we use all lowercase letters and go with the primary filename extension. |
| 1155 |
// For example "png", "jpg", "ico", "webp", etc |
| 1156 |
std::string_view id; |
| 1157 |
IsFormatCallback isFormat; |
| 1158 |
MakeFromStreamCallback makeFromStream; |
| 1159 |
}; |
| 1160 |
|
| 1161 |
// Add the decoder to the end of a linked list of decoders, which will be used to identify calls to |
| 1162 |
// SkCodec::MakeFromStream. If a decoder with the same id already exists, this new decoder |
| 1163 |
// will replace the existing one (in the same position). This is not thread-safe, so make sure all |
| 1164 |
// initialization is done before the first call. |
| 1165 |
void SK_API Register(Decoder d); |
| 1166 |
|
| 1167 |
/** |
| 1168 |
* Return a SkImage produced by the codec, but attempts to defer image allocation until the |
| 1169 |
* image is actually used/drawn. This deferral allows the system to cache the result, either on the |
| 1170 |
* CPU or on the GPU, depending on where the image is drawn. If memory is low, the cache may |
| 1171 |
* be purged, causing the next draw of the image to have to re-decode. |
| 1172 |
* |
| 1173 |
* If alphaType is nullopt, the image's alpha type will be chosen automatically based on the |
| 1174 |
* image format. Transparent images will default to kPremul_SkAlphaType. If alphaType contains |
| 1175 |
* kPremul_SkAlphaType or kUnpremul_SkAlphaType, that alpha type will be used. Forcing opaque |
| 1176 |
* (passing kOpaque_SkAlphaType) is not allowed, and will return nullptr. |
| 1177 |
* |
| 1178 |
* @param codec A non-null codec (e.g. from SkPngDecoder::Decode) |
| 1179 |
* @return created SkImage, or nullptr |
| 1180 |
*/ |
| 1181 |
SK_API sk_sp<SkImage> DeferredImage(std::unique_ptr<SkCodec> codec, |
| 1182 |
std::optional<SkAlphaType> alphaType = std::nullopt); |
| 1183 |
} |
| 1184 |
|
| 1185 |
#endif // SkCodec_DEFINED |