blob: 7a0ccb3a854b30b8c011c8761dc2e0a623309f3d [file] [log] [blame]
krajcevski99ffe242014-06-03 13:04:35 -07001/*
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 "SkColorPriv.h"
Hal Canary248ff022016-11-22 09:03:03 -07009#include "SkImageEncoderPriv.h"
msarett8715d472016-02-17 10:02:29 -080010#include "SkImageGenerator.h"
krajcevskic250d2e2014-06-06 06:16:28 -070011#include "SkPixelRef.h"
krajcevski99ffe242014-06-03 13:04:35 -070012#include "SkStream.h"
halcanary67ec1f82014-06-27 11:36:20 -070013#include "SkStreamPriv.h"
krajcevski99ffe242014-06-03 13:04:35 -070014#include "SkTypes.h"
15
16#include "ktx.h"
17#include "etc1.h"
18
msarett910f7ec2016-03-24 04:45:39 -070019///////////////////////////////////////////////////////////////////////////////
20
21// KTX Image Encoder
22//
msarette8597a42016-03-24 10:41:47 -070023// KTX is a general texture data storage file format ratified by the Khronos Group. As an
24// overview, a KTX file contains all of the appropriate values needed to fully specify a
25// texture in an OpenGL application, including the use of compressed data.
26//
krajcevskic250d2e2014-06-06 06:16:28 -070027// This encoder takes a best guess at how to encode the bitmap passed to it. If
28// there is an installed discardable pixel ref with existing PKM data, then we
29// will repurpose the existing ETC1 data into a KTX file. If the data contains
30// KTX data, then we simply return a copy of the same data. For all other files,
31// the underlying KTX library tries to do its best to encode the appropriate
32// data specified by the bitmap based on the config. (i.e. kAlpha8_Config will
33// be represented as a full resolution 8-bit image dump with the appropriate
34// OpenGL defines in the header).
35
36class SkKTXImageEncoder : public SkImageEncoder {
37protected:
mtklein36352bf2015-03-25 18:17:31 -070038 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override;
krajcevskic250d2e2014-06-06 06:16:28 -070039
40private:
41 virtual bool encodePKM(SkWStream* stream, const SkData *data);
42 typedef SkImageEncoder INHERITED;
43};
44
45bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int) {
scroggoc2dcf4a2014-07-02 15:00:07 -070046 if (!bitmap.pixelRef()) {
47 return false;
48 }
bungeman38d909e2016-08-02 14:40:46 -070049 sk_sp<SkData> data(bitmap.pixelRef()->refEncodedData());
krajcevskic250d2e2014-06-06 06:16:28 -070050
reedc9e190d2015-09-28 09:58:41 -070051 // Is this even encoded data?
52 if (data) {
53 const uint8_t *bytes = data->bytes();
54 if (etc1_pkm_is_valid(bytes)) {
bungeman38d909e2016-08-02 14:40:46 -070055 return this->encodePKM(stream, data.get());
krajcevskic250d2e2014-06-06 06:16:28 -070056 }
reedc9e190d2015-09-28 09:58:41 -070057
58 // Is it a KTX file??
scroggob8e09602016-04-12 07:41:22 -070059 if (SkKTXFile::is_ktx(bytes, data->size())) {
reedc9e190d2015-09-28 09:58:41 -070060 return stream->write(bytes, data->size());
61 }
halcanary9d524f22016-03-29 09:03:52 -070062
reedc9e190d2015-09-28 09:58:41 -070063 // If it's neither a KTX nor a PKM, then we need to
64 // get at the actual pixels, so fall through and decompress...
krajcevskic250d2e2014-06-06 06:16:28 -070065 }
reedc9e190d2015-09-28 09:58:41 -070066
krajcevskic250d2e2014-06-06 06:16:28 -070067 return SkKTXFile::WriteBitmapToKTX(stream, bitmap);
68}
69
70bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) {
71 const uint8_t* bytes = data->bytes();
72 SkASSERT(etc1_pkm_is_valid(bytes));
73
74 etc1_uint32 width = etc1_pkm_get_width(bytes);
75 etc1_uint32 height = etc1_pkm_get_height(bytes);
76
77 // ETC1 Data is stored as compressed 4x4 pixel blocks, so we must make sure
78 // that our dimensions are valid.
79 if (width == 0 || (width & 3) != 0 || height == 0 || (height & 3) != 0) {
80 return false;
81 }
82
83 // Advance pointer to etc1 data.
84 bytes += ETC_PKM_HEADER_SIZE;
85
86 return SkKTXFile::WriteETC1ToKTX(stream, bytes, width, height);
87}
88
krajcevski99ffe242014-06-03 13:04:35 -070089/////////////////////////////////////////////////////////////////////////////////////////
krajcevskic250d2e2014-06-06 06:16:28 -070090DEFINE_ENCODER_CREATOR(KTXImageEncoder);
krajcevski99ffe242014-06-03 13:04:35 -070091/////////////////////////////////////////////////////////////////////////////////////////
92
krajcevskic250d2e2014-06-06 06:16:28 -070093SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) {
Hal Canary248ff022016-11-22 09:03:03 -070094 return (SkEncodedImageFormat::kKTX == (SkEncodedImageFormat)t) ? new SkKTXImageEncoder : nullptr;
krajcevskic250d2e2014-06-06 06:16:28 -070095}
96
krajcevskic250d2e2014-06-06 06:16:28 -070097static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory);