hide lockpixels api behind flag
[platform/upstream/libSkiaSharp.git] / gm / encode-platform.cpp
1 /*
2  * Copyright 2016 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 "gm.h"
9
10 #include "Resources.h"
11 #include "SkCanvas.h"
12 #include "SkData.h"
13 #include "SkImageEncoderPriv.h"
14 #include "SkUnPreMultiply.h"
15
16 namespace skiagm {
17
18 static void make_opaque_256(SkBitmap* bitmap) {
19     GetResourceAsBitmap("mandrill_256.png", bitmap);
20 }
21
22 static void make_premul_256(SkBitmap* bitmap) {
23     SkBitmap tmp;
24     GetResourceAsBitmap("yellow_rose.png", &tmp);
25     tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256));
26 }
27
28 static void make_unpremul_256(SkBitmap* bitmap) {
29     make_premul_256(bitmap);
30     for (int y = 0; y < bitmap->height(); y++) {
31         for (int x = 0; x < bitmap->width(); x++) {
32             SkPMColor* pixel = bitmap->getAddr32(x, y);
33             *pixel = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*pixel);
34         }
35     }
36     bitmap->setAlphaType(kUnpremul_SkAlphaType);
37 }
38
39 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
40 static SkEncodedImageFormat kTypes[] {
41         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
42         SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
43 };
44 #elif defined(SK_BUILD_FOR_WIN)
45 // Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
46 static SkEncodedImageFormat kTypes[] {
47         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
48         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
49 };
50 #else
51 // Use WEBP in place of GIF.  Use PNG two extra times.  We don't support GIF, BMP, or ICO.
52 static SkEncodedImageFormat kTypes[] {
53         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
54         SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
55 };
56 #endif
57
58 static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
59     SkPixmap src;
60     if (!bitmap.peekPixels(&src)) {
61         return nullptr;
62     }
63     SkDynamicMemoryWStream buf;
64     #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
65         return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
66     #elif defined(SK_BUILD_FOR_WIN)
67         return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
68     #else
69         switch (type) {
70             case SkEncodedImageFormat::kPNG:
71                 return SkEncodeImageAsPNG(&buf, src, SkEncodeOptions()) ? buf.detachAsData()
72                                                                         : nullptr;
73             case SkEncodedImageFormat::kJPEG:
74                 return SkEncodeImageAsJPEG(&buf, src, 100) ? buf.detachAsData() : nullptr;
75             case SkEncodedImageFormat::kWEBP:
76                 return SkEncodeImageAsWEBP(&buf, src, 100) ? buf.detachAsData() : nullptr;
77             default:
78                 SkASSERT(false);
79                 return nullptr;
80         }
81     #endif
82 }
83
84 class EncodePlatformGM : public GM {
85 public:
86     EncodePlatformGM() {}
87
88 protected:
89     SkString onShortName() override {
90         return SkString("encode-platform");
91     }
92
93     SkISize onISize() override {
94         return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
95     }
96
97     void onDraw(SkCanvas* canvas) override {
98         SkBitmap opaqueBm, premulBm, unpremulBm;
99         make_opaque_256(&opaqueBm);
100         make_premul_256(&premulBm);
101         make_unpremul_256(&unpremulBm);
102
103         for (SkEncodedImageFormat type : kTypes) {
104             auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
105             auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
106             auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
107
108             canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
109             canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
110             canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
111
112             canvas->translate(256.0f, 0.0f);
113         }
114     }
115
116 private:
117     typedef GM INHERITED;
118 };
119
120 DEF_GM( return new EncodePlatformGM; )
121 }