blob: 522bd9447f7ef9e2a31252892b3dee28a7a17e37 [file] [log] [blame]
piotaixrd2a35222014-08-19 14:29:02 -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 "SkTypes.h"
reed7c748852014-11-10 08:57:21 -08009#include "Test.h"
10
piotaixrd2a35222014-08-19 14:29:02 -070011#if SK_SUPPORT_GPU
12#include "GrContextFactory.h"
13#endif
14#include "SkImage.h"
15#include "SkSurface.h"
reed7c748852014-11-10 08:57:21 -080016#include "SkReadBuffer.h"
17#include "SkWriteBuffer.h"
piotaixrd2a35222014-08-19 14:29:02 -070018
reed7c748852014-11-10 08:57:21 -080019static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) {
reed7534cf12014-12-15 06:41:02 -080020 // 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)];
reed7c748852014-11-10 08:57:21 -080022 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
37DEF_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}
piotaixrd2a35222014-08-19 14:29:02 -070050
reedb8881362014-08-20 07:24:01 -070051static 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
piotaixrd2a35222014-08-19 14:29:02 -070056DEF_TEST(ImageIsOpaqueTest, reporter) {
57 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
58 SkAutoTUnref<SkSurface> surfaceTransparent(SkSurface::NewRaster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070059 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070060
61 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
62 SkAutoTUnref<SkSurface> surfaceOpaque(SkSurface::NewRaster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070063 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070064}
65
66#if SK_SUPPORT_GPU
67
68DEF_GPUTEST(ImageIsOpaqueTest_GPU, reporter, factory) {
69 for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
70 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
71
72 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
73 continue;
74 }
75
76 GrContext* context = factory->get(glCtxType);
77
78 if (NULL == context) {
79 continue;
80 }
81
82 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
bsalomonafe30052015-01-16 07:32:33 -080083 SkAutoTUnref<SkSurface> surfaceTransparent(
84 SkSurface::NewRenderTarget(context,SkSurface::kNo_Budgeted, infoTransparent));
reedb8881362014-08-20 07:24:01 -070085 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070086
87 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
bsalomonafe30052015-01-16 07:32:33 -080088 SkAutoTUnref<SkSurface> surfaceOpaque(
89 SkSurface::NewRenderTarget(context,SkSurface::kNo_Budgeted, infoOpaque));
reedb8881362014-08-20 07:24:01 -070090#if 0
91 // this is failing right now : TODO fix me
92 check_isopaque(reporter, surfaceOpaque, true);
93#endif
piotaixrd2a35222014-08-19 14:29:02 -070094 }
95}
96
97#endif