blob: eb8f7615a9c47380efef87a1c4134df048abcfe1 [file] [log] [blame]
reed871872f2015-06-22 12:48:26 -07001/*
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#include "SkCanvas.h"
9#include "SkData.h"
10#include "SkDevice.h"
11#include "SkImageEncoder.h"
12#include "SkImage_Base.h"
13#include "SkRRect.h"
14#include "SkSurface.h"
15#include "SkUtils.h"
16#include "Test.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContextFactory.h"
20#include "GrTest.h"
21#include "gl/GrGLInterface.h"
22#include "gl/GrGLUtil.h"
23#else
24class GrContextFactory;
25class GrContext;
26#endif
27
28static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
29 SkImage* b) {
30 const int widthA = subsetA ? subsetA->width() : a->width();
31 const int heightA = subsetA ? subsetA->height() : a->height();
32
33 REPORTER_ASSERT(reporter, widthA == b->width());
34 REPORTER_ASSERT(reporter, heightA == b->height());
35#if 0
36 // see skbug.com/3965
37 bool AO = a->isOpaque();
38 bool BO = b->isOpaque();
39 REPORTER_ASSERT(reporter, AO == BO);
40#endif
41
42 SkImageInfo info = SkImageInfo::MakeN32(widthA, heightA,
43 a->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
44 SkAutoPixmapStorage pmapA, pmapB;
45 pmapA.alloc(info);
46 pmapB.alloc(info);
47
48 const int srcX = subsetA ? subsetA->x() : 0;
49 const int srcY = subsetA ? subsetA->y() : 0;
50
51 REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY));
52 REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0));
53
54 const size_t widthBytes = widthA * info.bytesPerPixel();
55 for (int y = 0; y < heightA; ++y) {
56 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
57 }
58}
59
60static SkImage* make_image(GrContext* ctx, int w, int h, const SkIRect& ir) {
61 const SkImageInfo info = SkImageInfo::MakeN32(w, h, kOpaque_SkAlphaType);
62 SkAutoTUnref<SkSurface> surface(ctx ?
63 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info) :
64 SkSurface::NewRaster(info));
65 SkCanvas* canvas = surface->getCanvas();
66 canvas->clear(SK_ColorWHITE);
67
68 SkPaint paint;
69 paint.setColor(SK_ColorBLACK);
70 canvas->drawRect(SkRect::Make(ir), paint);
71 return surface->newImageSnapshot();
72}
73
74static void test_encode(skiatest::Reporter* reporter, GrContext* ctx) {
75 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
76 SkAutoTUnref<SkImage> orig(make_image(ctx, 20, 20, ir));
77 SkAutoTUnref<SkData> origEncoded(orig->encode());
78 REPORTER_ASSERT(reporter, origEncoded);
79 REPORTER_ASSERT(reporter, origEncoded->size() > 0);
80
81 SkAutoTUnref<SkImage> decoded(SkImage::NewFromEncoded(origEncoded));
82 REPORTER_ASSERT(reporter, decoded);
83 assert_equal(reporter, orig, NULL, decoded);
84
85 // Now see if we can instantiate an image from a subset of the surface/origEncoded
86
87 decoded.reset(SkImage::NewFromEncoded(origEncoded, &ir));
88 REPORTER_ASSERT(reporter, decoded);
89 assert_equal(reporter, orig, &ir, decoded);
90}
91
92DEF_TEST(Image_Encode_Cpu, reporter) {
93 test_encode(reporter, NULL);
94}
95
96#if SK_SUPPORT_GPU
97DEF_GPUTEST(Image_Encode_Gpu, reporter, factory) {
98 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
99 if (!ctx) {
100 REPORTER_ASSERT(reporter, false);
101 return;
102 }
103 test_encode(reporter, ctx);
104}
105#endif
reed759373a2015-07-03 21:01:10 -0700106
107DEF_TEST(Image_NewRasterCopy, reporter) {
108 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
109 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
110 const SkPMColor blue = SkPackARGB32(0xFF, 0, 0, 0xFF);
111 SkPMColor colors[] = { red, green, blue, 0 };
112 SkAutoTUnref<SkColorTable> ctable(SkNEW_ARGS(SkColorTable, (colors, SK_ARRAY_COUNT(colors))));
113 // The colortable made a copy, so we can trash the original colors
114 memset(colors, 0xFF, sizeof(colors));
115
116 const SkImageInfo srcInfo = SkImageInfo::Make(2, 2, kIndex_8_SkColorType, kPremul_SkAlphaType);
117 const size_t srcRowBytes = 2 * sizeof(uint8_t);
118 uint8_t indices[] = { 0, 1, 2, 3 };
119 SkAutoTUnref<SkImage> image(SkImage::NewRasterCopy(srcInfo, indices, srcRowBytes, ctable));
120 // The image made a copy, so we can trash the original indices
121 memset(indices, 0xFF, sizeof(indices));
122
123 const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
124 const size_t dstRowBytes = 2 * sizeof(SkPMColor);
125 SkPMColor pixels[4];
126 memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
127 image->readPixels(dstInfo, pixels, dstRowBytes, 0, 0);
128 REPORTER_ASSERT(reporter, red == pixels[0]);
129 REPORTER_ASSERT(reporter, green == pixels[1]);
130 REPORTER_ASSERT(reporter, blue == pixels[2]);
131 REPORTER_ASSERT(reporter, 0 == pixels[3]);
132}