1 /*
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 "SkTypes.h"
9 #include "Test.h"
10
11 #if SK_SUPPORT_GPU
12 #include "GrContext.h"
13 #endif
14 #include "SkImage.h"
15 #include "SkSurface.h"
16 #include "SkReadBuffer.h"
17 #include "SkWriteBuffer.h"
18
test_flatten(skiatest::Reporter * reporter,const SkImageInfo & info)19 static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) {
20 // just need a safe amount of storage, but ensure that it is 4-byte aligned.
21 int32_t storage[(sizeof(SkImageInfo)*2) / sizeof(int32_t)];
22 SkWriteBuffer wb(storage, sizeof(storage));
23 info.flatten(wb);
24 SkASSERT(wb.bytesWritten() < sizeof(storage));
25
26 SkReadBuffer rb(storage, wb.bytesWritten());
27 SkImageInfo info2;
28
29 // pick a noisy byte pattern, so we ensure that unflatten sets all of our fields
30 memset(&info2, 0xB8, sizeof(info2));
31
32 info2.unflatten(rb);
33 REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten());
34 REPORTER_ASSERT(reporter, info == info2);
35 }
36
DEF_TEST(ImageInfo_flattening,reporter)37 DEF_TEST(ImageInfo_flattening, reporter) {
38 for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
39 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
40 for (int pt = 0; pt <= kLastEnum_SkColorProfileType; ++pt) {
41 SkImageInfo info = SkImageInfo::Make(100, 200,
42 static_cast<SkColorType>(ct),
43 static_cast<SkAlphaType>(at),
44 static_cast<SkColorProfileType>(pt));
45 test_flatten(reporter, info);
46 }
47 }
48 }
49 }
50
check_isopaque(skiatest::Reporter * reporter,SkSurface * surface,bool expectedOpaque)51 static void check_isopaque(skiatest::Reporter* reporter, SkSurface* surface, bool expectedOpaque) {
52 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
53 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
54 }
55
DEF_TEST(ImageIsOpaqueTest,reporter)56 DEF_TEST(ImageIsOpaqueTest, reporter) {
57 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
58 SkAutoTUnref<SkSurface> surfaceTransparent(SkSurface::NewRaster(infoTransparent));
59 check_isopaque(reporter, surfaceTransparent, false);
60
61 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
62 SkAutoTUnref<SkSurface> surfaceOpaque(SkSurface::NewRaster(infoOpaque));
63 check_isopaque(reporter, surfaceOpaque, true);
64 }
65
66 #if SK_SUPPORT_GPU
67
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,reporter,context)68 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, context) {
69 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
70 SkAutoTUnref<SkSurface> surfaceTransparent(
71 SkSurface::NewRenderTarget(context, SkBudgeted::kNo, infoTransparent));
72 check_isopaque(reporter, surfaceTransparent, false);
73
74 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
75 SkAutoTUnref<SkSurface> surfaceOpaque(
76 SkSurface::NewRenderTarget(context,SkBudgeted::kNo, infoOpaque));
77
78 check_isopaque(reporter, surfaceOpaque, true);
79 }
80
81 #endif
82