2014-08-19 21:29:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
2014-11-10 16:57:21 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2014-08-19 21:29:02 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "GrContext.h"
|
2014-08-19 21:29:02 +00:00
|
|
|
#endif
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkSurface.h"
|
2014-11-10 16:57:21 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2014-08-19 21:29:02 +00:00
|
|
|
|
2014-11-10 16:57:21 +00:00
|
|
|
static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) {
|
2014-12-15 14:41:02 +00:00
|
|
|
// just need a safe amount of storage, but ensure that it is 4-byte aligned.
|
|
|
|
int32_t storage[(sizeof(SkImageInfo)*2) / sizeof(int32_t)];
|
2014-11-10 16:57:21 +00:00
|
|
|
SkWriteBuffer wb(storage, sizeof(storage));
|
|
|
|
info.flatten(wb);
|
|
|
|
SkASSERT(wb.bytesWritten() < sizeof(storage));
|
|
|
|
|
|
|
|
SkReadBuffer rb(storage, wb.bytesWritten());
|
|
|
|
SkImageInfo info2;
|
|
|
|
|
|
|
|
// pick a noisy byte pattern, so we ensure that unflatten sets all of our fields
|
|
|
|
memset(&info2, 0xB8, sizeof(info2));
|
|
|
|
|
|
|
|
info2.unflatten(rb);
|
|
|
|
REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten());
|
|
|
|
REPORTER_ASSERT(reporter, info == info2);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ImageInfo_flattening, reporter) {
|
|
|
|
for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
|
|
|
|
for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
|
|
|
|
for (int pt = 0; pt <= kLastEnum_SkColorProfileType; ++pt) {
|
|
|
|
SkImageInfo info = SkImageInfo::Make(100, 200,
|
|
|
|
static_cast<SkColorType>(ct),
|
|
|
|
static_cast<SkAlphaType>(at),
|
|
|
|
static_cast<SkColorProfileType>(pt));
|
|
|
|
test_flatten(reporter, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 21:29:02 +00:00
|
|
|
|
2014-08-20 14:24:01 +00:00
|
|
|
static void check_isopaque(skiatest::Reporter* reporter, SkSurface* surface, bool expectedOpaque) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(surface->makeImageSnapshot());
|
2014-08-20 14:24:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
|
|
|
|
}
|
|
|
|
|
2014-08-19 21:29:02 +00:00
|
|
|
DEF_TEST(ImageIsOpaqueTest, reporter) {
|
|
|
|
SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
SkAutoTUnref<SkSurface> surfaceTransparent(SkSurface::NewRaster(infoTransparent));
|
2014-08-20 14:24:01 +00:00
|
|
|
check_isopaque(reporter, surfaceTransparent, false);
|
2014-08-19 21:29:02 +00:00
|
|
|
|
|
|
|
SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
|
|
|
|
SkAutoTUnref<SkSurface> surfaceOpaque(SkSurface::NewRaster(infoOpaque));
|
2014-08-20 14:24:01 +00:00
|
|
|
check_isopaque(reporter, surfaceOpaque, true);
|
2014-08-19 21:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, context) {
|
|
|
|
SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
SkAutoTUnref<SkSurface> surfaceTransparent(
|
2016-02-25 16:33:02 +00:00
|
|
|
SkSurface::NewRenderTarget(context, SkBudgeted::kNo, infoTransparent));
|
2015-12-01 12:35:26 +00:00
|
|
|
check_isopaque(reporter, surfaceTransparent, false);
|
2014-08-19 21:29:02 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
|
|
|
|
SkAutoTUnref<SkSurface> surfaceOpaque(
|
2016-02-25 16:33:02 +00:00
|
|
|
SkSurface::NewRenderTarget(context,SkBudgeted::kNo, infoOpaque));
|
2015-10-01 16:49:25 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
check_isopaque(reporter, surfaceOpaque, true);
|
2014-08-19 21:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|