blob: d513e89ad27025d7d1a20700dbe354af83be8ec2 [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)];
brianosmanfad98562016-05-04 11:06:28 -070022 SkBinaryWriteBuffer wb(storage, sizeof(storage));
reed7c748852014-11-10 08:57:21 -080023 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
reede8f30622016-03-23 18:59:25 -070051static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
52 bool expectedOpaque) {
reed9ce9d672016-03-17 10:51:11 -070053 sk_sp<SkImage> image(surface->makeImageSnapshot());
reedb8881362014-08-20 07:24:01 -070054 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
55}
56
piotaixrd2a35222014-08-19 14:29:02 -070057DEF_TEST(ImageIsOpaqueTest, reporter) {
58 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070059 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070060 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070061
62 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070063 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070064 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070065}
66
67#if SK_SUPPORT_GPU
68
bsalomon758586c2016-04-06 14:02:39 -070069DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070070 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080071 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070072 auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
kkinnunen15302832015-12-01 04:35:26 -080073 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070074
kkinnunen15302832015-12-01 04:35:26 -080075 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070076 auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, 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