blob: 5d7003f20b9baa13956fd520d59a6bb99cdd502a [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
kkinnunen15302832015-12-01 04:35:26 -080012#include "GrContext.h"
piotaixrd2a35222014-08-19 14:29:02 -070013#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
kkinnunen15302832015-12-01 04:35:26 -080068DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, context) {
69 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
70 SkAutoTUnref<SkSurface> surfaceTransparent(
71 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, infoTransparent));
72 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070073
kkinnunen15302832015-12-01 04:35:26 -080074 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
75 SkAutoTUnref<SkSurface> surfaceOpaque(
76 SkSurface::NewRenderTarget(context,SkSurface::kNo_Budgeted, infoOpaque));
piotaixrd2a35222014-08-19 14:29:02 -070077
kkinnunen15302832015-12-01 04:35:26 -080078 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070079}
80
81#endif