blob: b196ca8f349bbd23149c97afe28f06a095bd1c83 [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