2013-03-28 13:49:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-01-10 22:08:27 +00:00
|
|
|
|
2013-03-28 13:49:22 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-02-06 14:11:56 +00:00
|
|
|
#include "SkData.h"
|
2015-06-09 19:16:53 +00:00
|
|
|
#include "SkDevice.h"
|
2014-02-06 14:11:56 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2015-06-10 15:49:28 +00:00
|
|
|
#include "SkImage_Base.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2013-03-28 13:49:22 +00:00
|
|
|
#include "SkRRect.h"
|
|
|
|
#include "SkSurface.h"
|
2014-02-06 14:11:56 +00:00
|
|
|
#include "SkUtils.h"
|
2013-03-28 13:49:22 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContextFactory.h"
|
2015-06-16 15:07:16 +00:00
|
|
|
#include "GrTest.h"
|
2013-03-28 13:49:22 +00:00
|
|
|
#else
|
|
|
|
class GrContextFactory;
|
|
|
|
class GrContext;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum SurfaceType {
|
|
|
|
kRaster_SurfaceType,
|
2014-02-13 17:14:46 +00:00
|
|
|
kRasterDirect_SurfaceType,
|
2013-03-28 13:49:22 +00:00
|
|
|
kGpu_SurfaceType,
|
2014-03-19 21:19:16 +00:00
|
|
|
kGpuScratch_SurfaceType,
|
2015-06-23 21:38:48 +00:00
|
|
|
|
|
|
|
kLastSurfaceType = kGpuScratch_SurfaceType
|
2013-03-28 13:49:22 +00:00
|
|
|
};
|
2015-06-23 21:38:48 +00:00
|
|
|
static const int kSurfaceTypeCnt = kLastSurfaceType + 1;
|
2013-03-28 13:49:22 +00:00
|
|
|
|
2014-06-27 13:48:14 +00:00
|
|
|
static void release_storage(void* pixels, void* context) {
|
|
|
|
SkASSERT(pixels == context);
|
|
|
|
sk_free(pixels);
|
|
|
|
}
|
2014-02-13 17:14:46 +00:00
|
|
|
|
2015-06-23 21:38:48 +00:00
|
|
|
static SkSurface* create_surface(SurfaceType surfaceType, GrContext* context,
|
|
|
|
SkAlphaType at = kPremul_SkAlphaType,
|
2015-08-27 14:41:13 +00:00
|
|
|
SkImageInfo* requestedInfo = nullptr) {
|
2015-06-23 21:38:48 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at);
|
2014-02-13 17:14:46 +00:00
|
|
|
|
|
|
|
if (requestedInfo) {
|
|
|
|
*requestedInfo = info;
|
|
|
|
}
|
2013-03-28 13:49:22 +00:00
|
|
|
|
|
|
|
switch (surfaceType) {
|
2014-02-13 17:14:46 +00:00
|
|
|
case kRaster_SurfaceType:
|
|
|
|
return SkSurface::NewRaster(info);
|
2014-06-27 13:48:14 +00:00
|
|
|
case kRasterDirect_SurfaceType: {
|
|
|
|
const size_t rowBytes = info.minRowBytes();
|
|
|
|
void* storage = sk_malloc_throw(info.getSafeSize(rowBytes));
|
|
|
|
return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes,
|
|
|
|
release_storage, storage);
|
|
|
|
}
|
2014-02-13 17:14:46 +00:00
|
|
|
case kGpu_SurfaceType:
|
2015-08-27 14:41:13 +00:00
|
|
|
return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr);
|
2014-03-19 21:19:16 +00:00
|
|
|
case kGpuScratch_SurfaceType:
|
2015-08-27 14:41:13 +00:00
|
|
|
return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, nullptr);
|
2013-03-28 13:49:22 +00:00
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2013-03-28 13:49:22 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:11:56 +00:00
|
|
|
enum ImageType {
|
|
|
|
kRasterCopy_ImageType,
|
|
|
|
kRasterData_ImageType,
|
2015-06-18 20:41:40 +00:00
|
|
|
kRasterProc_ImageType,
|
2014-02-06 14:11:56 +00:00
|
|
|
kGpu_ImageType,
|
|
|
|
kCodec_ImageType,
|
|
|
|
};
|
2014-02-06 13:43:07 +00:00
|
|
|
|
2014-12-31 20:31:43 +00:00
|
|
|
#include "SkImageGenerator.h"
|
|
|
|
|
|
|
|
class EmptyGenerator : public SkImageGenerator {
|
2015-03-19 15:31:14 +00:00
|
|
|
public:
|
|
|
|
EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
|
2014-12-31 20:31:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void test_empty_image(skiatest::Reporter* reporter) {
|
|
|
|
const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterCopy(info, nullptr, 0));
|
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterData(info, nullptr, 0));
|
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromRaster(info, nullptr, 0, nullptr, nullptr));
|
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromGenerator(new EmptyGenerator));
|
2014-12-31 20:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) {
|
|
|
|
const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRaster(info));
|
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRasterDirect(info, nullptr, 0));
|
2014-12-31 20:31:43 +00:00
|
|
|
if (ctx) {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr ==
|
|
|
|
SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, nullptr));
|
2014-12-31 20:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:38:40 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-07-08 19:54:04 +00:00
|
|
|
static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext* ctx) {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == ctx) {
|
2015-04-08 15:38:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-16 15:07:16 +00:00
|
|
|
|
2015-07-13 14:19:57 +00:00
|
|
|
const GrGpu* gpu = ctx->getGpu();
|
|
|
|
if (!gpu) {
|
2015-06-16 15:07:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-13 14:19:57 +00:00
|
|
|
// Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in
|
2015-07-08 19:54:04 +00:00
|
|
|
// a SkSurface.
|
|
|
|
static const int kW = 100;
|
|
|
|
static const int kH = 100;
|
|
|
|
static const uint32_t kOrigColor = 0xFFAABBCC;
|
|
|
|
SkAutoTArray<uint32_t> pixels(kW * kH);
|
|
|
|
sk_memset32(pixels.get(), kOrigColor, kW * kH);
|
2015-07-14 18:02:52 +00:00
|
|
|
GrBackendObject texID = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH,
|
|
|
|
kRGBA_8888_GrPixelConfig);
|
2015-07-08 19:54:04 +00:00
|
|
|
|
|
|
|
GrBackendTextureDesc wrappedDesc;
|
|
|
|
wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
wrappedDesc.fWidth = kW;
|
|
|
|
wrappedDesc.fHeight = kH;
|
|
|
|
wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
|
|
|
wrappedDesc.fSampleCnt = 0;
|
|
|
|
wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
|
|
|
|
wrappedDesc.fTextureHandle = texID;
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrappedDesc, nullptr));
|
2015-07-08 19:54:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, surface);
|
|
|
|
if (surface) {
|
|
|
|
// Validate that we can draw to the canvas and that the original texture color is preserved
|
|
|
|
// in pixels that aren't rendered to via the surface.
|
|
|
|
SkPaint paint;
|
|
|
|
static const SkColor kRectColor = ~kOrigColor | 0xFF000000;
|
|
|
|
paint.setColor(kRectColor);
|
|
|
|
surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2),
|
|
|
|
paint);
|
|
|
|
SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH);
|
|
|
|
surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0);
|
|
|
|
bool stop = false;
|
|
|
|
SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF),
|
|
|
|
(kOrigColor >> 0 & 0xFF),
|
|
|
|
(kOrigColor >> 8 & 0xFF),
|
|
|
|
(kOrigColor >> 16 & 0xFF));
|
|
|
|
SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF),
|
|
|
|
(kRectColor >> 16 & 0xFF),
|
|
|
|
(kRectColor >> 8 & 0xFF),
|
|
|
|
(kRectColor >> 0 & 0xFF));
|
|
|
|
for (int y = 0; y < kH/2 && !stop; ++y) {
|
|
|
|
for (int x = 0; x < kW && !stop; ++x) {
|
|
|
|
REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]);
|
|
|
|
if (rectColorPM != pixels[x + y * kW]) {
|
|
|
|
stop = true;
|
2015-06-16 15:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 19:54:04 +00:00
|
|
|
}
|
|
|
|
stop = false;
|
|
|
|
for (int y = kH/2; y < kH && !stop; ++y) {
|
|
|
|
for (int x = 0; x < kW && !stop; ++x) {
|
|
|
|
REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]);
|
|
|
|
if (origColorPM != pixels[x + y * kW]) {
|
|
|
|
stop = true;
|
2015-06-16 15:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 15:38:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-02-06 13:43:07 +00:00
|
|
|
static void test_image(skiatest::Reporter* reporter) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
|
|
|
|
size_t rowBytes = info.minRowBytes();
|
|
|
|
size_t size = info.getSafeSize(rowBytes);
|
2014-09-12 19:12:27 +00:00
|
|
|
SkData* data = SkData::NewUninitialized(size);
|
2014-02-07 12:20:04 +00:00
|
|
|
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, data->unique());
|
2014-02-06 13:43:07 +00:00
|
|
|
SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !data->unique());
|
2014-02-06 13:43:07 +00:00
|
|
|
image->unref();
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, data->unique());
|
2014-02-06 13:43:07 +00:00
|
|
|
data->unref();
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:41:40 +00:00
|
|
|
// Want to ensure that our Release is called when the owning image is destroyed
|
|
|
|
struct ReleaseDataContext {
|
|
|
|
skiatest::Reporter* fReporter;
|
|
|
|
SkData* fData;
|
|
|
|
|
|
|
|
static void Release(const void* pixels, void* context) {
|
|
|
|
ReleaseDataContext* state = (ReleaseDataContext*)context;
|
|
|
|
REPORTER_ASSERT(state->fReporter, state->fData);
|
|
|
|
state->fData->unref();
|
2015-08-27 14:41:13 +00:00
|
|
|
state->fData = nullptr;
|
2015-06-18 20:41:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-29 21:09:41 +00:00
|
|
|
// May we (soon) eliminate the need to keep testing this, by hiding the bloody device!
|
|
|
|
#include "SkDevice.h"
|
|
|
|
static uint32_t get_legacy_gen_id(SkSurface* surf) {
|
|
|
|
SkBaseDevice* device = surf->getCanvas()->getDevice_just_for_deprecated_compatibility_testing();
|
|
|
|
return device->accessBitmap(false).getGenerationID();
|
|
|
|
}
|
|
|
|
|
2015-07-03 17:43:43 +00:00
|
|
|
/*
|
|
|
|
* Test legacy behavor of bumping the surface's device's bitmap's genID when we access its
|
|
|
|
* texture handle for writing.
|
|
|
|
*
|
|
|
|
* Note: this needs to be tested separately from checking newImageSnapshot, as calling that
|
|
|
|
* can also incidentally bump the genID (when a new backing surface is created).
|
|
|
|
*/
|
2015-07-08 19:54:04 +00:00
|
|
|
template <class F>
|
|
|
|
static void test_texture_handle_genID(skiatest::Reporter* reporter, SkSurface* surf, F f) {
|
2015-07-03 17:43:43 +00:00
|
|
|
const uint32_t gen0 = get_legacy_gen_id(surf);
|
2015-07-08 19:54:04 +00:00
|
|
|
f(surf, SkSurface::kFlushRead_BackendHandleAccess);
|
2015-07-03 17:43:43 +00:00
|
|
|
const uint32_t gen1 = get_legacy_gen_id(surf);
|
|
|
|
REPORTER_ASSERT(reporter, gen0 == gen1);
|
|
|
|
|
2015-07-08 19:54:04 +00:00
|
|
|
f(surf, SkSurface::kFlushWrite_BackendHandleAccess);
|
2015-07-03 17:43:43 +00:00
|
|
|
const uint32_t gen2 = get_legacy_gen_id(surf);
|
|
|
|
REPORTER_ASSERT(reporter, gen0 != gen2);
|
|
|
|
|
2015-07-08 19:54:04 +00:00
|
|
|
f(surf, SkSurface::kDiscardWrite_BackendHandleAccess);
|
2015-07-03 17:43:43 +00:00
|
|
|
const uint32_t gen3 = get_legacy_gen_id(surf);
|
|
|
|
REPORTER_ASSERT(reporter, gen0 != gen3);
|
|
|
|
REPORTER_ASSERT(reporter, gen2 != gen3);
|
|
|
|
}
|
|
|
|
|
2015-07-08 19:54:04 +00:00
|
|
|
template <class F>
|
|
|
|
static void test_backend_handle(skiatest::Reporter* reporter, SkSurface* surf, F f) {
|
2015-06-29 14:37:01 +00:00
|
|
|
SkAutoTUnref<SkImage> image0(surf->newImageSnapshot());
|
2015-07-08 19:54:04 +00:00
|
|
|
GrBackendObject obj = f(surf, SkSurface::kFlushRead_BackendHandleAccess);
|
2015-06-29 14:37:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj != 0);
|
|
|
|
SkAutoTUnref<SkImage> image1(surf->newImageSnapshot());
|
|
|
|
// just read access should not affect the snapshot
|
|
|
|
REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID());
|
|
|
|
|
2015-07-08 19:54:04 +00:00
|
|
|
obj = f(surf, SkSurface::kFlushWrite_BackendHandleAccess);
|
2015-06-29 14:37:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj != 0);
|
|
|
|
SkAutoTUnref<SkImage> image2(surf->newImageSnapshot());
|
|
|
|
// expect a new image, since we claimed we would write
|
|
|
|
REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID());
|
|
|
|
|
2015-07-08 19:54:04 +00:00
|
|
|
obj = f(surf, SkSurface::kDiscardWrite_BackendHandleAccess);
|
2015-06-29 14:37:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj != 0);
|
|
|
|
SkAutoTUnref<SkImage> image3(surf->newImageSnapshot());
|
|
|
|
// expect a new(er) image, since we claimed we would write
|
|
|
|
REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID());
|
|
|
|
REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID());
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkImage* create_image(skiatest::Reporter* reporter,
|
|
|
|
ImageType imageType, GrContext* context, SkColor color,
|
2015-06-23 21:38:48 +00:00
|
|
|
ReleaseDataContext* releaseContext) {
|
2014-02-06 14:11:56 +00:00
|
|
|
const SkPMColor pmcolor = SkPreMultiplyColor(color);
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
|
|
|
|
const size_t rowBytes = info.minRowBytes();
|
2014-09-03 18:54:58 +00:00
|
|
|
const size_t size = rowBytes * info.height();
|
2014-02-06 14:11:56 +00:00
|
|
|
|
2014-09-12 19:12:27 +00:00
|
|
|
SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
|
|
|
|
void* addr = data->writable_data();
|
2014-02-06 14:11:56 +00:00
|
|
|
sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
|
|
|
|
|
|
|
|
switch (imageType) {
|
|
|
|
case kRasterCopy_ImageType:
|
|
|
|
return SkImage::NewRasterCopy(info, addr, rowBytes);
|
|
|
|
case kRasterData_ImageType:
|
|
|
|
return SkImage::NewRasterData(info, data, rowBytes);
|
2015-06-18 20:41:40 +00:00
|
|
|
case kRasterProc_ImageType:
|
|
|
|
SkASSERT(releaseContext);
|
|
|
|
releaseContext->fData = SkRef(data.get());
|
|
|
|
return SkImage::NewFromRaster(info, addr, rowBytes,
|
|
|
|
ReleaseDataContext::Release, releaseContext);
|
2014-12-10 14:54:06 +00:00
|
|
|
case kGpu_ImageType: {
|
2015-01-16 15:32:33 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf(
|
|
|
|
SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0));
|
2014-12-10 14:54:06 +00:00
|
|
|
surf->getCanvas()->clear(color);
|
2015-07-08 19:54:04 +00:00
|
|
|
// test our backing texture / rendertarget while were here...
|
|
|
|
auto textureAccessorFunc =
|
|
|
|
[](SkSurface* surf, SkSurface::BackendHandleAccess access) -> GrBackendObject {
|
|
|
|
return surf->getTextureHandle(access); };
|
|
|
|
auto renderTargetAccessorFunc =
|
|
|
|
[](SkSurface* surf, SkSurface::BackendHandleAccess access) -> GrBackendObject {
|
|
|
|
GrBackendObject obj;
|
|
|
|
SkAssertResult(surf->getRenderTargetHandle(&obj, access));
|
|
|
|
return obj; };
|
|
|
|
test_backend_handle(reporter, surf, textureAccessorFunc);
|
|
|
|
test_backend_handle(reporter, surf, renderTargetAccessorFunc);
|
|
|
|
test_texture_handle_genID(reporter, surf, textureAccessorFunc);
|
|
|
|
test_texture_handle_genID(reporter, surf, renderTargetAccessorFunc);
|
|
|
|
|
2015-06-29 14:37:01 +00:00
|
|
|
// redraw so our returned image looks as expected.
|
|
|
|
surf->getCanvas()->clear(color);
|
2014-12-10 14:54:06 +00:00
|
|
|
return surf->newImageSnapshot();
|
|
|
|
}
|
2014-02-06 14:11:56 +00:00
|
|
|
case kCodec_ImageType: {
|
|
|
|
SkBitmap bitmap;
|
2014-05-29 15:57:20 +00:00
|
|
|
bitmap.installPixels(info, addr, rowBytes);
|
2014-02-06 14:11:56 +00:00
|
|
|
SkAutoTUnref<SkData> src(
|
2014-12-10 14:54:06 +00:00
|
|
|
SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100));
|
2015-06-22 19:48:26 +00:00
|
|
|
return SkImage::NewFromEncoded(src);
|
2014-02-06 14:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SkASSERT(false);
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-02-06 14:11:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 17:53:42 +00:00
|
|
|
static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) {
|
|
|
|
sk_memset32(pixels, color, count);
|
|
|
|
}
|
|
|
|
static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (pixels[i] != expected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image,
|
|
|
|
SkPMColor expected) {
|
|
|
|
const SkPMColor notExpected = ~expected;
|
|
|
|
|
|
|
|
const int w = 2, h = 2;
|
|
|
|
const size_t rowBytes = w * sizeof(SkPMColor);
|
|
|
|
SkPMColor pixels[w*h];
|
|
|
|
|
|
|
|
SkImageInfo info;
|
|
|
|
|
|
|
|
info = SkImageInfo::MakeUnknown(w, h);
|
|
|
|
REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
|
|
|
|
|
|
|
|
// out-of-bounds should fail
|
|
|
|
info = SkImageInfo::MakeN32Premul(w, h);
|
|
|
|
REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
|
|
|
|
REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
|
|
|
|
REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
|
|
|
|
REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
|
|
|
|
|
|
|
|
// top-left should succeed
|
|
|
|
set_pixels(pixels, w*h, notExpected);
|
|
|
|
REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
|
|
|
|
REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
|
|
|
|
|
|
|
|
// bottom-right should succeed
|
|
|
|
set_pixels(pixels, w*h, notExpected);
|
|
|
|
REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
|
|
|
|
image->width() - w, image->height() - h));
|
|
|
|
REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
|
|
|
|
|
|
|
|
// partial top-left should succeed
|
|
|
|
set_pixels(pixels, w*h, notExpected);
|
|
|
|
REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
|
|
|
|
REPORTER_ASSERT(reporter, pixels[3] == expected);
|
|
|
|
REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
|
|
|
|
|
|
|
|
// partial bottom-right should succeed
|
|
|
|
set_pixels(pixels, w*h, notExpected);
|
|
|
|
REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
|
|
|
|
image->width() - 1, image->height() - 1));
|
|
|
|
REPORTER_ASSERT(reporter, pixels[0] == expected);
|
|
|
|
REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
|
|
|
|
}
|
|
|
|
|
2015-07-10 00:10:31 +00:00
|
|
|
static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image,
|
|
|
|
const SkBitmap& bitmap, SkImage::LegacyBitmapMode mode) {
|
|
|
|
REPORTER_ASSERT(reporter, image->width() == bitmap.width());
|
|
|
|
REPORTER_ASSERT(reporter, image->height() == bitmap.height());
|
|
|
|
REPORTER_ASSERT(reporter, image->isOpaque() == bitmap.isOpaque());
|
|
|
|
|
|
|
|
if (SkImage::kRO_LegacyBitmapMode == mode) {
|
|
|
|
REPORTER_ASSERT(reporter, bitmap.isImmutable());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoLockPixels alp(bitmap);
|
|
|
|
REPORTER_ASSERT(reporter, bitmap.getPixels());
|
|
|
|
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
|
|
|
|
SkPMColor imageColor;
|
|
|
|
REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0));
|
|
|
|
REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
|
|
|
|
}
|
|
|
|
|
2015-07-08 19:46:22 +00:00
|
|
|
static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) {
|
|
|
|
const SkImage::LegacyBitmapMode modes[] = {
|
|
|
|
SkImage::kRO_LegacyBitmapMode,
|
|
|
|
SkImage::kRW_LegacyBitmapMode,
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, modes[i]));
|
2015-07-10 00:10:31 +00:00
|
|
|
check_legacy_bitmap(reporter, image, bitmap, modes[i]);
|
|
|
|
|
|
|
|
// Test subsetting to exercise the rowBytes logic.
|
|
|
|
SkBitmap tmp;
|
|
|
|
REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
|
|
|
|
image->height() / 2)));
|
|
|
|
SkAutoTUnref<SkImage> subsetImage(SkImage::NewFromBitmap(tmp));
|
|
|
|
REPORTER_ASSERT(reporter, subsetImage);
|
|
|
|
|
|
|
|
SkBitmap subsetBitmap;
|
|
|
|
REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, modes[i]));
|
|
|
|
check_legacy_bitmap(reporter, subsetImage, subsetBitmap, modes[i]);
|
2015-07-08 19:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 14:54:06 +00:00
|
|
|
static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
2014-02-06 14:11:56 +00:00
|
|
|
static const struct {
|
|
|
|
ImageType fType;
|
|
|
|
bool fPeekShouldSucceed;
|
2014-12-10 14:54:06 +00:00
|
|
|
const char* fName;
|
2014-02-06 14:11:56 +00:00
|
|
|
} gRec[] = {
|
2014-12-10 14:54:06 +00:00
|
|
|
{ kRasterCopy_ImageType, true, "RasterCopy" },
|
|
|
|
{ kRasterData_ImageType, true, "RasterData" },
|
2015-06-18 20:41:40 +00:00
|
|
|
{ kRasterProc_ImageType, true, "RasterProc" },
|
2014-12-10 14:54:06 +00:00
|
|
|
{ kGpu_ImageType, false, "Gpu" },
|
|
|
|
{ kCodec_ImageType, false, "Codec" },
|
2014-02-06 14:11:56 +00:00
|
|
|
};
|
2014-02-14 03:02:05 +00:00
|
|
|
|
2014-02-06 14:11:56 +00:00
|
|
|
const SkColor color = SK_ColorRED;
|
|
|
|
const SkPMColor pmcolor = SkPreMultiplyColor(color);
|
2014-02-14 03:02:05 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
GrContext* ctx = nullptr;
|
2014-12-10 14:54:06 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
ctx = factory->get(GrContextFactory::kNative_GLContextType);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == ctx) {
|
2015-04-21 13:59:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-10 14:54:06 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-18 20:41:40 +00:00
|
|
|
ReleaseDataContext releaseCtx;
|
|
|
|
releaseCtx.fReporter = reporter;
|
|
|
|
|
2014-02-06 14:11:56 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
|
|
|
|
SkImageInfo info;
|
|
|
|
size_t rowBytes;
|
2014-02-14 03:02:05 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
releaseCtx.fData = nullptr;
|
2015-06-29 14:37:01 +00:00
|
|
|
SkAutoTUnref<SkImage> image(create_image(reporter, gRec[i].fType, ctx, color, &releaseCtx));
|
2014-02-06 14:11:56 +00:00
|
|
|
if (!image.get()) {
|
2014-12-10 14:54:06 +00:00
|
|
|
SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName);
|
2014-02-06 14:11:56 +00:00
|
|
|
continue; // gpu may not be enabled
|
|
|
|
}
|
2015-06-18 20:41:40 +00:00
|
|
|
if (kRasterProc_ImageType == gRec[i].fType) {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr != releaseCtx.fData); // we are tracking the data
|
2015-06-18 20:41:40 +00:00
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == releaseCtx.fData); // we ignored the context
|
2015-06-18 20:41:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 19:46:22 +00:00
|
|
|
test_legacy_bitmap(reporter, image);
|
|
|
|
|
2014-02-06 14:11:56 +00:00
|
|
|
const void* addr = image->peekPixels(&info, &rowBytes);
|
2014-09-05 20:34:00 +00:00
|
|
|
bool success = SkToBool(addr);
|
2014-02-06 14:11:56 +00:00
|
|
|
REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
|
|
|
|
if (success) {
|
2014-09-03 18:54:58 +00:00
|
|
|
REPORTER_ASSERT(reporter, 10 == info.width());
|
|
|
|
REPORTER_ASSERT(reporter, 10 == info.height());
|
|
|
|
REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
|
|
|
|
REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
|
|
|
|
kOpaque_SkAlphaType == info.alphaType());
|
2014-02-06 14:11:56 +00:00
|
|
|
REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
|
|
|
|
REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
|
|
|
|
}
|
2014-12-10 17:53:42 +00:00
|
|
|
|
|
|
|
test_image_readpixels(reporter, image, pmcolor);
|
2014-02-06 14:11:56 +00:00
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == releaseCtx.fData); // we released the data
|
2014-02-06 14:11:56 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
static void test_canvaspeek(skiatest::Reporter* reporter,
|
|
|
|
GrContextFactory* factory) {
|
|
|
|
static const struct {
|
|
|
|
SurfaceType fType;
|
|
|
|
bool fPeekShouldSucceed;
|
|
|
|
} gRec[] = {
|
|
|
|
{ kRaster_SurfaceType, true },
|
|
|
|
{ kRasterDirect_SurfaceType, true },
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
{ kGpu_SurfaceType, false },
|
2014-03-19 21:19:16 +00:00
|
|
|
{ kGpuScratch_SurfaceType, false },
|
2014-02-13 17:14:46 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkColor color = SK_ColorRED;
|
|
|
|
const SkPMColor pmcolor = SkPreMultiplyColor(color);
|
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
int cnt;
|
2014-02-13 17:14:46 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-07-17 17:50:59 +00:00
|
|
|
cnt = GrContextFactory::kGLContextTypeCnt;
|
|
|
|
#else
|
|
|
|
cnt = 1;
|
2014-02-13 17:14:46 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
for (int i= 0; i < cnt; ++i) {
|
2015-08-27 14:41:13 +00:00
|
|
|
GrContext* context = nullptr;
|
2014-07-17 17:50:59 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
context = factory->get(glCtxType);
|
2014-02-13 17:14:46 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2014-07-17 17:50:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
|
|
|
|
SkImageInfo info, requestInfo;
|
|
|
|
size_t rowBytes;
|
|
|
|
|
2015-06-23 21:38:48 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(create_surface(gRec[i].fType, context,
|
|
|
|
kPremul_SkAlphaType, &requestInfo));
|
2014-07-17 17:50:59 +00:00
|
|
|
surface->getCanvas()->clear(color);
|
|
|
|
|
|
|
|
const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
|
2014-09-05 20:34:00 +00:00
|
|
|
bool success = SkToBool(addr);
|
2014-07-17 17:50:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
|
|
|
|
|
|
|
|
SkImageInfo info2;
|
|
|
|
size_t rb2;
|
|
|
|
const void* addr2 = surface->peekPixels(&info2, &rb2);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
REPORTER_ASSERT(reporter, requestInfo == info);
|
|
|
|
REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
|
|
|
|
REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, addr2 == addr);
|
|
|
|
REPORTER_ASSERT(reporter, info2 == info);
|
|
|
|
REPORTER_ASSERT(reporter, rb2 == rowBytes);
|
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == addr2);
|
2014-07-17 17:50:59 +00:00
|
|
|
}
|
2014-02-13 17:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:16:53 +00:00
|
|
|
// For compatibility with clients that still call accessBitmap(), we need to ensure that we bump
|
|
|
|
// the bitmap's genID when we draw to it, else they won't know it has new values. When they are
|
|
|
|
// exclusively using surface/image, and we can hide accessBitmap from device, we can remove this
|
|
|
|
// test.
|
|
|
|
static void test_accessPixels(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
|
|
|
static const struct {
|
|
|
|
SurfaceType fType;
|
|
|
|
bool fPeekShouldSucceed;
|
|
|
|
} gRec[] = {
|
|
|
|
{ kRaster_SurfaceType, true },
|
|
|
|
{ kRasterDirect_SurfaceType, true },
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
{ kGpu_SurfaceType, false },
|
|
|
|
{ kGpuScratch_SurfaceType, false },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
int cnt;
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
cnt = GrContextFactory::kGLContextTypeCnt;
|
|
|
|
#else
|
|
|
|
cnt = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int i= 0; i < cnt; ++i) {
|
2015-08-27 14:41:13 +00:00
|
|
|
GrContext* context = nullptr;
|
2015-06-09 19:16:53 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
context = factory->get(glCtxType);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2015-06-09 19:16:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gRec); ++j) {
|
|
|
|
SkImageInfo info, requestInfo;
|
|
|
|
|
2015-06-23 21:38:48 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(create_surface(gRec[j].fType, context,
|
|
|
|
kPremul_SkAlphaType, &requestInfo));
|
2015-06-09 19:16:53 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(0);
|
|
|
|
|
|
|
|
SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing();
|
|
|
|
SkBitmap bm = device->accessBitmap(false);
|
|
|
|
uint32_t genID0 = bm.getGenerationID();
|
|
|
|
// Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write)
|
|
|
|
canvas->drawColor(SK_ColorBLUE);
|
|
|
|
// Now check that we get a different genID
|
|
|
|
uint32_t genID1 = bm.getGenerationID();
|
|
|
|
REPORTER_ASSERT(reporter, genID0 != genID1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:38:48 +00:00
|
|
|
static void test_snap_alphatype(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
2015-08-27 14:41:13 +00:00
|
|
|
GrContext* context = nullptr;
|
2015-06-23 21:38:48 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
context = factory->get(GrContextFactory::kNative_GLContextType);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2015-06-23 21:38:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (int opaque = 0; opaque < 2; ++opaque) {
|
|
|
|
SkAlphaType atype = SkToBool(opaque) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
|
|
|
|
for (int st = 0; st < kSurfaceTypeCnt; ++st) {
|
|
|
|
SurfaceType stype = (SurfaceType)st;
|
|
|
|
SkAutoTUnref<SkSurface> surface(create_surface(stype, context, atype));
|
|
|
|
REPORTER_ASSERT(reporter, surface);
|
|
|
|
if (surface) {
|
|
|
|
SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
|
|
|
|
REPORTER_ASSERT(reporter, image);
|
|
|
|
if (image) {
|
|
|
|
REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(opaque));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 14:04:51 +00:00
|
|
|
static void test_backend_cow(skiatest::Reporter* reporter, SkSurface* surface,
|
|
|
|
SkSurface::BackendHandleAccess mode,
|
|
|
|
GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) {
|
|
|
|
GrBackendObject obj1 = func(surface, mode);
|
|
|
|
SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot());
|
|
|
|
|
|
|
|
GrBackendObject obj2 = func(surface, mode);
|
|
|
|
SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot());
|
|
|
|
|
|
|
|
// If the access mode triggers CoW, then the backend objects should reflect it.
|
|
|
|
REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2));
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:49:22 +00:00
|
|
|
static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
|
|
|
|
GrContext* context) {
|
|
|
|
// Verify that the right canvas commands trigger a copy on write
|
2015-06-23 21:38:48 +00:00
|
|
|
SkSurface* surface = create_surface(surfaceType, context);
|
2013-03-28 13:49:22 +00:00
|
|
|
SkAutoTUnref<SkSurface> aur_surface(surface);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
const SkRect testRect =
|
|
|
|
SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(4), SkIntToScalar(5));
|
|
|
|
SkPath testPath;
|
|
|
|
testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(1)));
|
|
|
|
|
|
|
|
const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
|
|
|
|
|
|
|
|
SkRegion testRegion;
|
|
|
|
testRegion.setRect(testIRect);
|
|
|
|
|
|
|
|
|
|
|
|
const SkColor testColor = 0x01020304;
|
|
|
|
const SkPaint testPaint;
|
|
|
|
const SkPoint testPoints[3] = {
|
|
|
|
{SkIntToScalar(0), SkIntToScalar(0)},
|
|
|
|
{SkIntToScalar(2), SkIntToScalar(1)},
|
|
|
|
{SkIntToScalar(0), SkIntToScalar(2)}
|
|
|
|
};
|
|
|
|
const size_t testPointCount = 3;
|
|
|
|
|
|
|
|
SkBitmap testBitmap;
|
2014-02-13 14:41:43 +00:00
|
|
|
testBitmap.allocN32Pixels(10, 10);
|
2013-10-09 12:51:09 +00:00
|
|
|
testBitmap.eraseColor(0);
|
2013-03-28 13:49:22 +00:00
|
|
|
|
|
|
|
SkRRect testRRect;
|
|
|
|
testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
|
|
|
|
|
|
|
|
SkString testText("Hello World");
|
|
|
|
const SkPoint testPoints2[] = {
|
|
|
|
{ SkIntToScalar(0), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(1), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(2), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(3), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(4), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(5), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(6), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(7), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(8), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(9), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(10), SkIntToScalar(1) },
|
|
|
|
};
|
|
|
|
|
|
|
|
#define EXPECT_COPY_ON_WRITE(command) \
|
|
|
|
{ \
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* imageBefore = surface->newImageSnapshot(); \
|
2013-03-28 13:49:22 +00:00
|
|
|
SkAutoTUnref<SkImage> aur_before(imageBefore); \
|
|
|
|
canvas-> command ; \
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* imageAfter = surface->newImageSnapshot(); \
|
2013-03-28 13:49:22 +00:00
|
|
|
SkAutoTUnref<SkImage> aur_after(imageAfter); \
|
|
|
|
REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_COPY_ON_WRITE(clear(testColor))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
|
|
|
|
testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
|
2015-08-06 17:02:53 +00:00
|
|
|
EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr))
|
2015-08-27 14:41:13 +00:00
|
|
|
EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, nullptr))
|
2013-03-28 13:49:22 +00:00
|
|
|
EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
|
|
|
|
EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
|
|
|
|
testPaint))
|
2015-08-27 14:41:13 +00:00
|
|
|
EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, nullptr, \
|
2013-03-28 13:49:22 +00:00
|
|
|
testPaint))
|
2015-08-06 14:04:51 +00:00
|
|
|
|
|
|
|
const SkSurface::BackendHandleAccess accessModes[] = {
|
|
|
|
SkSurface::kFlushRead_BackendHandleAccess,
|
|
|
|
SkSurface::kFlushWrite_BackendHandleAccess,
|
|
|
|
SkSurface::kDiscardWrite_BackendHandleAccess,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto access : accessModes) {
|
|
|
|
test_backend_cow(reporter, surface, access,
|
|
|
|
[](SkSurface* s, SkSurface::BackendHandleAccess a) -> GrBackendObject {
|
|
|
|
return s->getTextureHandle(a);
|
|
|
|
});
|
|
|
|
|
|
|
|
test_backend_cow(reporter, surface, access,
|
|
|
|
[](SkSurface* s, SkSurface::BackendHandleAccess a) -> GrBackendObject {
|
|
|
|
GrBackendObject result;
|
|
|
|
if (!s->getRenderTargetHandle(&result, a)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
2013-03-28 13:49:22 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:03:26 +00:00
|
|
|
static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
|
|
|
|
SurfaceType surfaceType,
|
|
|
|
GrContext* context) {
|
|
|
|
// This test succeeds by not triggering an assertion.
|
|
|
|
// The test verifies that the surface remains writable (usable) after
|
|
|
|
// acquiring and releasing a snapshot without triggering a copy on write.
|
2015-06-23 21:38:48 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context));
|
2013-04-03 15:03:26 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(1);
|
2013-04-12 20:20:50 +00:00
|
|
|
surface->newImageSnapshot()->unref(); // Create and destroy SkImage
|
2013-07-25 23:29:40 +00:00
|
|
|
canvas->clear(2); // Must not assert internally
|
2013-03-28 13:49:22 +00:00
|
|
|
}
|
2013-05-01 22:38:16 +00:00
|
|
|
|
2013-05-01 22:49:59 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2013-07-25 23:29:40 +00:00
|
|
|
static void Test_crbug263329(skiatest::Reporter* reporter,
|
2014-03-19 21:19:16 +00:00
|
|
|
SurfaceType surfaceType,
|
2013-07-25 23:29:40 +00:00
|
|
|
GrContext* context) {
|
|
|
|
// This is a regression test for crbug.com/263329
|
|
|
|
// Bug was caused by onCopyOnWrite releasing the old surface texture
|
|
|
|
// back to the scratch texture pool even though the texture is used
|
|
|
|
// by and active SkImage_Gpu.
|
2015-06-23 21:38:48 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface1(create_surface(surfaceType, context));
|
|
|
|
SkAutoTUnref<SkSurface> surface2(create_surface(surfaceType, context));
|
2013-07-25 23:29:40 +00:00
|
|
|
SkCanvas* canvas1 = surface1->getCanvas();
|
|
|
|
SkCanvas* canvas2 = surface2->getCanvas();
|
|
|
|
canvas1->clear(1);
|
|
|
|
SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
|
|
|
|
// Trigger copy on write, new backing is a scratch texture
|
|
|
|
canvas1->clear(2);
|
|
|
|
SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
|
|
|
|
// Trigger copy on write, old backing should not be returned to scratch
|
|
|
|
// pool because it is held by image2
|
|
|
|
canvas1->clear(3);
|
|
|
|
|
|
|
|
canvas2->clear(4);
|
|
|
|
SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
|
|
|
|
// Trigger copy on write on surface2. The new backing store should not
|
|
|
|
// be recycling a texture that is held by an existing image.
|
|
|
|
canvas2->clear(5);
|
|
|
|
SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
|
2015-06-10 15:49:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture());
|
2013-07-25 23:29:40 +00:00
|
|
|
// The following assertion checks crbug.com/263329
|
2015-06-10 15:49:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture());
|
|
|
|
REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture());
|
|
|
|
REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture());
|
|
|
|
REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture());
|
|
|
|
REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture());
|
2013-07-25 23:29:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 22:38:16 +00:00
|
|
|
static void TestGetTexture(skiatest::Reporter* reporter,
|
|
|
|
SurfaceType surfaceType,
|
|
|
|
GrContext* context) {
|
2015-06-23 21:38:48 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context));
|
2013-05-01 22:38:16 +00:00
|
|
|
SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
|
2015-06-10 15:49:28 +00:00
|
|
|
GrTexture* texture = as_IB(image)->getTexture();
|
2014-03-19 21:19:16 +00:00
|
|
|
if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) {
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture);
|
2013-05-01 22:38:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
|
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == texture);
|
2013-05-01 22:38:16 +00:00
|
|
|
}
|
|
|
|
surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
|
2015-06-10 15:49:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture);
|
2013-05-01 22:38:16 +00:00
|
|
|
}
|
2015-01-23 16:08:04 +00:00
|
|
|
|
2015-02-13 22:20:05 +00:00
|
|
|
#include "GrGpuResourcePriv.h"
|
2015-01-23 16:08:04 +00:00
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#include "SkImage_Gpu.h"
|
|
|
|
#include "SkSurface_Gpu.h"
|
|
|
|
|
|
|
|
SkSurface::Budgeted is_budgeted(SkSurface* surf) {
|
2015-02-13 22:20:05 +00:00
|
|
|
return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted() ?
|
2015-01-23 16:08:04 +00:00
|
|
|
SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSurface::Budgeted is_budgeted(SkImage* image) {
|
2015-02-13 22:20:05 +00:00
|
|
|
return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ?
|
2015-01-23 16:08:04 +00:00
|
|
|
SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(8,8);
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
|
|
|
SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted;
|
|
|
|
SkAutoTUnref<SkSurface>
|
|
|
|
surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0));
|
|
|
|
SkASSERT(surface);
|
|
|
|
REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
|
|
|
|
|
2015-01-24 19:27:27 +00:00
|
|
|
SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted));
|
2015-01-23 16:08:04 +00:00
|
|
|
|
|
|
|
// Initially the image shares a texture with the surface, and the surface decides
|
|
|
|
// whether it is budgeted or not.
|
|
|
|
REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
|
|
|
|
REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image));
|
|
|
|
|
|
|
|
// Now trigger copy-on-write
|
|
|
|
surface->getCanvas()->clear(SK_ColorBLUE);
|
|
|
|
|
|
|
|
// They don't share a texture anymore. They should each have made their own budget
|
|
|
|
// decision.
|
|
|
|
REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface));
|
|
|
|
REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 22:49:59 +00:00
|
|
|
#endif
|
2013-05-01 22:38:16 +00:00
|
|
|
|
2013-04-16 19:41:09 +00:00
|
|
|
static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
|
|
|
|
SurfaceType surfaceType,
|
2013-04-22 14:28:01 +00:00
|
|
|
GrContext* context,
|
|
|
|
SkSurface::ContentChangeMode mode) {
|
2013-04-16 19:41:09 +00:00
|
|
|
// Verifies the robustness of SkSurface for handling use cases where calls
|
|
|
|
// are made before a canvas is created.
|
|
|
|
{
|
|
|
|
// Test passes by not asserting
|
2015-06-23 21:38:48 +00:00
|
|
|
SkSurface* surface = create_surface(surfaceType, context);
|
2013-04-16 19:41:09 +00:00
|
|
|
SkAutoTUnref<SkSurface> aur_surface(surface);
|
2013-04-22 14:28:01 +00:00
|
|
|
surface->notifyContentWillChange(mode);
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(surface->validate();)
|
2013-04-16 19:41:09 +00:00
|
|
|
}
|
|
|
|
{
|
2015-06-23 21:38:48 +00:00
|
|
|
SkSurface* surface = create_surface(surfaceType, context);
|
2013-04-16 19:41:09 +00:00
|
|
|
SkAutoTUnref<SkSurface> aur_surface(surface);
|
|
|
|
SkImage* image1 = surface->newImageSnapshot();
|
|
|
|
SkAutoTUnref<SkImage> aur_image1(image1);
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(image1->validate();)
|
|
|
|
SkDEBUGCODE(surface->validate();)
|
2013-04-22 14:28:01 +00:00
|
|
|
surface->notifyContentWillChange(mode);
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(image1->validate();)
|
|
|
|
SkDEBUGCODE(surface->validate();)
|
2013-04-16 19:41:09 +00:00
|
|
|
SkImage* image2 = surface->newImageSnapshot();
|
|
|
|
SkAutoTUnref<SkImage> aur_image2(image2);
|
2013-10-02 16:42:21 +00:00
|
|
|
SkDEBUGCODE(image2->validate();)
|
|
|
|
SkDEBUGCODE(surface->validate();)
|
2013-04-16 19:41:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, image1 != image2);
|
|
|
|
}
|
2013-04-17 07:00:56 +00:00
|
|
|
|
2013-04-16 19:41:09 +00:00
|
|
|
}
|
2013-03-28 13:49:22 +00:00
|
|
|
|
2014-01-10 22:08:27 +00:00
|
|
|
DEF_GPUTEST(Surface, reporter, factory) {
|
2014-02-06 13:43:07 +00:00
|
|
|
test_image(reporter);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, nullptr);
|
|
|
|
TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, nullptr);
|
|
|
|
TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, nullptr, SkSurface::kDiscard_ContentChangeMode);
|
|
|
|
TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, nullptr, SkSurface::kRetain_ContentChangeMode);
|
2014-02-13 17:14:46 +00:00
|
|
|
|
2014-12-31 20:31:43 +00:00
|
|
|
test_empty_image(reporter);
|
2015-08-27 14:41:13 +00:00
|
|
|
test_empty_surface(reporter, nullptr);
|
2014-12-31 20:31:43 +00:00
|
|
|
|
2014-12-10 14:54:06 +00:00
|
|
|
test_imagepeek(reporter, factory);
|
2014-02-13 17:14:46 +00:00
|
|
|
test_canvaspeek(reporter, factory);
|
|
|
|
|
2015-06-09 19:16:53 +00:00
|
|
|
test_accessPixels(reporter, factory);
|
|
|
|
|
2015-06-23 21:38:48 +00:00
|
|
|
test_snap_alphatype(reporter, factory);
|
|
|
|
|
2013-05-01 22:49:59 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-08-27 14:41:13 +00:00
|
|
|
TestGetTexture(reporter, kRaster_SurfaceType, nullptr);
|
2014-09-05 20:34:00 +00:00
|
|
|
if (factory) {
|
2014-07-17 17:50:59 +00:00
|
|
|
for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
|
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(glCtxType);
|
2014-09-05 20:34:00 +00:00
|
|
|
if (context) {
|
2014-07-17 17:50:59 +00:00
|
|
|
Test_crbug263329(reporter, kGpu_SurfaceType, context);
|
|
|
|
Test_crbug263329(reporter, kGpuScratch_SurfaceType, context);
|
|
|
|
TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
|
|
|
|
TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context);
|
|
|
|
TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
|
|
|
|
TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context);
|
|
|
|
TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
|
|
|
|
TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
|
|
|
|
TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
|
|
|
|
TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
|
|
|
|
TestGetTexture(reporter, kGpu_SurfaceType, context);
|
|
|
|
TestGetTexture(reporter, kGpuScratch_SurfaceType, context);
|
2014-12-31 20:31:43 +00:00
|
|
|
test_empty_surface(reporter, context);
|
2015-01-23 16:08:04 +00:00
|
|
|
test_surface_budget(reporter, context);
|
2015-07-08 19:54:04 +00:00
|
|
|
test_wrapped_texture_surface(reporter, context);
|
2014-07-17 17:50:59 +00:00
|
|
|
}
|
2013-11-12 13:51:03 +00:00
|
|
|
}
|
2013-04-03 15:03:26 +00:00
|
|
|
}
|
2013-03-28 13:49:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-05-07 22:36:17 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2015-06-18 20:41:40 +00:00
|
|
|
|
|
|
|
struct ReleaseTextureContext {
|
|
|
|
ReleaseTextureContext(skiatest::Reporter* reporter) {
|
|
|
|
fReporter = reporter;
|
|
|
|
fIsReleased = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
skiatest::Reporter* fReporter;
|
|
|
|
bool fIsReleased;
|
|
|
|
|
|
|
|
void doRelease() {
|
|
|
|
REPORTER_ASSERT(fReporter, false == fIsReleased);
|
|
|
|
fIsReleased = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReleaseProc(void* context) {
|
|
|
|
((ReleaseTextureContext*)context)->doRelease();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static SkImage* make_desc_image(GrContext* ctx, int w, int h, GrBackendObject texID,
|
|
|
|
ReleaseTextureContext* releaseContext) {
|
2015-05-07 22:36:17 +00:00
|
|
|
GrBackendTextureDesc desc;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
// need to be a rendertarget for now...
|
|
|
|
desc.fFlags = kRenderTarget_GrBackendTextureFlag;
|
|
|
|
desc.fWidth = w;
|
|
|
|
desc.fHeight = h;
|
|
|
|
desc.fSampleCnt = 0;
|
|
|
|
desc.fTextureHandle = texID;
|
2015-06-18 20:41:40 +00:00
|
|
|
return releaseContext
|
|
|
|
? SkImage::NewFromTexture(ctx, desc, kPremul_SkAlphaType,
|
|
|
|
ReleaseTextureContext::ReleaseProc, releaseContext)
|
|
|
|
: SkImage::NewFromTextureCopy(ctx, desc, kPremul_SkAlphaType);
|
2015-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_image_color(skiatest::Reporter* reporter, SkImage* image, SkPMColor expected) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
|
|
|
|
SkPMColor pixel;
|
|
|
|
REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0, 0));
|
|
|
|
REPORTER_ASSERT(reporter, pixel == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) {
|
|
|
|
GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
|
|
|
|
if (!ctx) {
|
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrTextureProvider* provider = ctx->textureProvider();
|
|
|
|
|
|
|
|
const int w = 10;
|
|
|
|
const int h = 10;
|
|
|
|
SkPMColor storage[w * h];
|
|
|
|
const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED);
|
|
|
|
sk_memset32(storage, expected0, w * h);
|
|
|
|
|
|
|
|
GrSurfaceDesc desc;
|
2015-07-16 17:00:28 +00:00
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget for readpixels();
|
2015-05-07 22:36:17 +00:00
|
|
|
desc.fOrigin = kDefault_GrSurfaceOrigin;
|
|
|
|
desc.fWidth = w;
|
|
|
|
desc.fHeight = h;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
desc.fSampleCnt = 0;
|
|
|
|
|
|
|
|
SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w * 4));
|
|
|
|
if (!tex) {
|
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
return;
|
|
|
|
}
|
2015-06-18 20:41:40 +00:00
|
|
|
|
2015-05-07 22:36:17 +00:00
|
|
|
GrBackendObject srcTex = tex->getTextureHandle();
|
2015-06-18 20:41:40 +00:00
|
|
|
ReleaseTextureContext releaseCtx(reporter);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkImage> refImg(make_desc_image(ctx, w, h, srcTex, &releaseCtx));
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkImage> cpyImg(make_desc_image(ctx, w, h, srcTex, nullptr));
|
2015-05-07 22:36:17 +00:00
|
|
|
|
|
|
|
test_image_color(reporter, refImg, expected0);
|
|
|
|
test_image_color(reporter, cpyImg, expected0);
|
|
|
|
|
|
|
|
// Now lets jam new colors into our "external" texture, and see if the images notice
|
|
|
|
const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE);
|
|
|
|
sk_memset32(storage, expected1, w * h);
|
|
|
|
tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kFlushWrites_PixelOp);
|
|
|
|
|
2015-07-23 19:22:19 +00:00
|
|
|
// The cpy'd one should still see the old color
|
|
|
|
#if 0
|
|
|
|
// There is no guarantee that refImg sees the new color. We are free to have made a copy. Our
|
|
|
|
// write pixels call violated the contract with refImg and refImg is now undefined.
|
2015-05-07 22:36:17 +00:00
|
|
|
test_image_color(reporter, refImg, expected1);
|
2015-07-23 19:22:19 +00:00
|
|
|
#endif
|
2015-05-07 22:36:17 +00:00
|
|
|
test_image_color(reporter, cpyImg, expected0);
|
2015-06-18 20:41:40 +00:00
|
|
|
|
|
|
|
// Now exercise the release proc
|
|
|
|
REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased);
|
2015-08-27 14:41:13 +00:00
|
|
|
refImg.reset(nullptr); // force a release of the image
|
2015-06-18 20:41:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, releaseCtx.fIsReleased);
|
2015-05-07 22:36:17 +00:00
|
|
|
}
|
|
|
|
#endif
|