junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 7 | |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 10 | #include "SkDevice.h" |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 11 | #include "SkImageEncoder.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 12 | #include "SkImage_Base.h" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 13 | #include "SkPath.h" |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
| 15 | #include "SkSurface.h" |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 16 | #include "SkUtils.h" |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 17 | #include "Test.h" |
| 18 | |
| 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContextFactory.h" |
bsalomon | 7a61793 | 2015-06-16 08:07:16 -0700 | [diff] [blame] | 21 | #include "GrTest.h" |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 22 | #else |
| 23 | class GrContextFactory; |
| 24 | class GrContext; |
| 25 | #endif |
| 26 | |
| 27 | enum SurfaceType { |
| 28 | kRaster_SurfaceType, |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 29 | kRasterDirect_SurfaceType, |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 30 | kGpu_SurfaceType, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 31 | kGpuScratch_SurfaceType, |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 32 | |
| 33 | kLastSurfaceType = kGpuScratch_SurfaceType |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 34 | }; |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 35 | static const int kSurfaceTypeCnt = kLastSurfaceType + 1; |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 36 | |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 37 | static void release_storage(void* pixels, void* context) { |
| 38 | SkASSERT(pixels == context); |
| 39 | sk_free(pixels); |
| 40 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 41 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 42 | static SkSurface* create_surface(SurfaceType surfaceType, GrContext* context, |
| 43 | SkAlphaType at = kPremul_SkAlphaType, |
| 44 | SkImageInfo* requestedInfo = NULL) { |
| 45 | const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 46 | |
| 47 | if (requestedInfo) { |
| 48 | *requestedInfo = info; |
| 49 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 50 | |
| 51 | switch (surfaceType) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 52 | case kRaster_SurfaceType: |
| 53 | return SkSurface::NewRaster(info); |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 54 | case kRasterDirect_SurfaceType: { |
| 55 | const size_t rowBytes = info.minRowBytes(); |
| 56 | void* storage = sk_malloc_throw(info.getSafeSize(rowBytes)); |
| 57 | return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes, |
| 58 | release_storage, storage); |
| 59 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 60 | case kGpu_SurfaceType: |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 61 | return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, NULL); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 62 | case kGpuScratch_SurfaceType: |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 63 | return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, NULL); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 64 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
| 67 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 68 | enum ImageType { |
| 69 | kRasterCopy_ImageType, |
| 70 | kRasterData_ImageType, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 71 | kRasterProc_ImageType, |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 72 | kGpu_ImageType, |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 73 | kCodec_ImageType, |
| 74 | }; |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 75 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 76 | #include "SkImageGenerator.h" |
| 77 | |
| 78 | class EmptyGenerator : public SkImageGenerator { |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 79 | public: |
| 80 | EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static void test_empty_image(skiatest::Reporter* reporter) { |
| 84 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
| 85 | |
| 86 | REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterCopy(info, NULL, 0)); |
| 87 | REPORTER_ASSERT(reporter, NULL == SkImage::NewRasterData(info, NULL, 0)); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 88 | REPORTER_ASSERT(reporter, NULL == SkImage::NewFromRaster(info, NULL, 0, NULL, NULL)); |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 89 | REPORTER_ASSERT(reporter, NULL == SkImage::NewFromGenerator(SkNEW(EmptyGenerator))); |
| 90 | } |
| 91 | |
| 92 | static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) { |
| 93 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
| 94 | |
| 95 | REPORTER_ASSERT(reporter, NULL == SkSurface::NewRaster(info)); |
| 96 | REPORTER_ASSERT(reporter, NULL == SkSurface::NewRasterDirect(info, NULL, 0)); |
| 97 | if (ctx) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, NULL == |
| 99 | SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, NULL)); |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 103 | #if SK_SUPPORT_GPU |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 104 | static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext* ctx) { |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 105 | if (NULL == ctx) { |
| 106 | return; |
| 107 | } |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 108 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 109 | const GrGpu* gpu = ctx->getGpu(); |
| 110 | if (!gpu) { |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
bsalomon | 7a61793 | 2015-06-16 08:07:16 -0700 | [diff] [blame] | 113 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 114 | // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 115 | // a SkSurface. |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 116 | static const int kW = 100; |
| 117 | static const int kH = 100; |
| 118 | static const uint32_t kOrigColor = 0xFFAABBCC; |
| 119 | SkAutoTArray<uint32_t> pixels(kW * kH); |
| 120 | sk_memset32(pixels.get(), kOrigColor, kW * kH); |
jvanverth | 8895792 | 2015-07-14 11:02:52 -0700 | [diff] [blame] | 121 | GrBackendObject texID = gpu->createTestingOnlyBackendTexture(pixels.get(), kW, kH, |
| 122 | kRGBA_8888_GrPixelConfig); |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 123 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 124 | GrBackendTextureDesc wrappedDesc; |
| 125 | wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 126 | wrappedDesc.fWidth = kW; |
| 127 | wrappedDesc.fHeight = kH; |
| 128 | wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 129 | wrappedDesc.fSampleCnt = 0; |
| 130 | wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 131 | wrappedDesc.fTextureHandle = texID; |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 132 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 133 | SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrappedDesc, NULL)); |
| 134 | REPORTER_ASSERT(reporter, surface); |
| 135 | if (surface) { |
| 136 | // Validate that we can draw to the canvas and that the original texture color is preserved |
| 137 | // in pixels that aren't rendered to via the surface. |
| 138 | SkPaint paint; |
| 139 | static const SkColor kRectColor = ~kOrigColor | 0xFF000000; |
| 140 | paint.setColor(kRectColor); |
| 141 | surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntToScalar(kH)/2), |
| 142 | paint); |
| 143 | SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH); |
| 144 | surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0); |
| 145 | bool stop = false; |
| 146 | SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF), |
| 147 | (kOrigColor >> 0 & 0xFF), |
| 148 | (kOrigColor >> 8 & 0xFF), |
| 149 | (kOrigColor >> 16 & 0xFF)); |
| 150 | SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF), |
| 151 | (kRectColor >> 16 & 0xFF), |
| 152 | (kRectColor >> 8 & 0xFF), |
| 153 | (kRectColor >> 0 & 0xFF)); |
| 154 | for (int y = 0; y < kH/2 && !stop; ++y) { |
| 155 | for (int x = 0; x < kW && !stop; ++x) { |
| 156 | REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]); |
| 157 | if (rectColorPM != pixels[x + y * kW]) { |
| 158 | stop = true; |
bsalomon | 7a61793 | 2015-06-16 08:07:16 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 162 | stop = false; |
| 163 | for (int y = kH/2; y < kH && !stop; ++y) { |
| 164 | for (int x = 0; x < kW && !stop; ++x) { |
| 165 | REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]); |
| 166 | if (origColorPM != pixels[x + y * kW]) { |
| 167 | stop = true; |
| 168 | } |
| 169 | } |
bsalomon | 7a61793 | 2015-06-16 08:07:16 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 172 | } |
| 173 | #endif |
| 174 | |
| 175 | |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 176 | static void test_image(skiatest::Reporter* reporter) { |
| 177 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 178 | size_t rowBytes = info.minRowBytes(); |
| 179 | size_t size = info.getSafeSize(rowBytes); |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 180 | SkData* data = SkData::NewUninitialized(size); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 181 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 182 | REPORTER_ASSERT(reporter, data->unique()); |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 183 | SkImage* image = SkImage::NewRasterData(info, data, rowBytes); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 184 | REPORTER_ASSERT(reporter, !data->unique()); |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 185 | image->unref(); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 186 | REPORTER_ASSERT(reporter, data->unique()); |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 187 | data->unref(); |
| 188 | } |
| 189 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 190 | // Want to ensure that our Release is called when the owning image is destroyed |
| 191 | struct ReleaseDataContext { |
| 192 | skiatest::Reporter* fReporter; |
| 193 | SkData* fData; |
| 194 | |
| 195 | static void Release(const void* pixels, void* context) { |
| 196 | ReleaseDataContext* state = (ReleaseDataContext*)context; |
| 197 | REPORTER_ASSERT(state->fReporter, state->fData); |
| 198 | state->fData->unref(); |
| 199 | state->fData = NULL; |
| 200 | } |
| 201 | }; |
| 202 | |
reed | 09b2c93 | 2015-06-29 14:09:41 -0700 | [diff] [blame] | 203 | // May we (soon) eliminate the need to keep testing this, by hiding the bloody device! |
| 204 | #include "SkDevice.h" |
| 205 | static uint32_t get_legacy_gen_id(SkSurface* surf) { |
| 206 | SkBaseDevice* device = surf->getCanvas()->getDevice_just_for_deprecated_compatibility_testing(); |
| 207 | return device->accessBitmap(false).getGenerationID(); |
| 208 | } |
| 209 | |
reed | dca20ce | 2015-07-03 10:43:43 -0700 | [diff] [blame] | 210 | /* |
| 211 | * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its |
| 212 | * texture handle for writing. |
| 213 | * |
| 214 | * Note: this needs to be tested separately from checking newImageSnapshot, as calling that |
| 215 | * can also incidentally bump the genID (when a new backing surface is created). |
| 216 | */ |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 217 | template <class F> |
| 218 | static void test_texture_handle_genID(skiatest::Reporter* reporter, SkSurface* surf, F f) { |
reed | dca20ce | 2015-07-03 10:43:43 -0700 | [diff] [blame] | 219 | const uint32_t gen0 = get_legacy_gen_id(surf); |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 220 | f(surf, SkSurface::kFlushRead_BackendHandleAccess); |
reed | dca20ce | 2015-07-03 10:43:43 -0700 | [diff] [blame] | 221 | const uint32_t gen1 = get_legacy_gen_id(surf); |
| 222 | REPORTER_ASSERT(reporter, gen0 == gen1); |
| 223 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 224 | f(surf, SkSurface::kFlushWrite_BackendHandleAccess); |
reed | dca20ce | 2015-07-03 10:43:43 -0700 | [diff] [blame] | 225 | const uint32_t gen2 = get_legacy_gen_id(surf); |
| 226 | REPORTER_ASSERT(reporter, gen0 != gen2); |
| 227 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 228 | f(surf, SkSurface::kDiscardWrite_BackendHandleAccess); |
reed | dca20ce | 2015-07-03 10:43:43 -0700 | [diff] [blame] | 229 | const uint32_t gen3 = get_legacy_gen_id(surf); |
| 230 | REPORTER_ASSERT(reporter, gen0 != gen3); |
| 231 | REPORTER_ASSERT(reporter, gen2 != gen3); |
| 232 | } |
| 233 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 234 | template <class F> |
| 235 | static void test_backend_handle(skiatest::Reporter* reporter, SkSurface* surf, F f) { |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 236 | SkAutoTUnref<SkImage> image0(surf->newImageSnapshot()); |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 237 | GrBackendObject obj = f(surf, SkSurface::kFlushRead_BackendHandleAccess); |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 238 | REPORTER_ASSERT(reporter, obj != 0); |
| 239 | SkAutoTUnref<SkImage> image1(surf->newImageSnapshot()); |
| 240 | // just read access should not affect the snapshot |
| 241 | REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID()); |
| 242 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 243 | obj = f(surf, SkSurface::kFlushWrite_BackendHandleAccess); |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 244 | REPORTER_ASSERT(reporter, obj != 0); |
| 245 | SkAutoTUnref<SkImage> image2(surf->newImageSnapshot()); |
| 246 | // expect a new image, since we claimed we would write |
| 247 | REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID()); |
| 248 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 249 | obj = f(surf, SkSurface::kDiscardWrite_BackendHandleAccess); |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 250 | REPORTER_ASSERT(reporter, obj != 0); |
| 251 | SkAutoTUnref<SkImage> image3(surf->newImageSnapshot()); |
| 252 | // expect a new(er) image, since we claimed we would write |
| 253 | REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID()); |
| 254 | REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID()); |
| 255 | } |
| 256 | |
| 257 | static SkImage* create_image(skiatest::Reporter* reporter, |
| 258 | ImageType imageType, GrContext* context, SkColor color, |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 259 | ReleaseDataContext* releaseContext) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 260 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 261 | const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 262 | const size_t rowBytes = info.minRowBytes(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 263 | const size_t size = rowBytes * info.height(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 264 | |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 265 | SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
| 266 | void* addr = data->writable_data(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 267 | sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 268 | |
| 269 | switch (imageType) { |
| 270 | case kRasterCopy_ImageType: |
| 271 | return SkImage::NewRasterCopy(info, addr, rowBytes); |
| 272 | case kRasterData_ImageType: |
| 273 | return SkImage::NewRasterData(info, data, rowBytes); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 274 | case kRasterProc_ImageType: |
| 275 | SkASSERT(releaseContext); |
| 276 | releaseContext->fData = SkRef(data.get()); |
| 277 | return SkImage::NewFromRaster(info, addr, rowBytes, |
| 278 | ReleaseDataContext::Release, releaseContext); |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 279 | case kGpu_ImageType: { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 280 | SkAutoTUnref<SkSurface> surf( |
| 281 | SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0)); |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 282 | surf->getCanvas()->clear(color); |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 283 | // test our backing texture / rendertarget while were here... |
| 284 | auto textureAccessorFunc = |
| 285 | [](SkSurface* surf, SkSurface::BackendHandleAccess access) -> GrBackendObject { |
| 286 | return surf->getTextureHandle(access); }; |
| 287 | auto renderTargetAccessorFunc = |
| 288 | [](SkSurface* surf, SkSurface::BackendHandleAccess access) -> GrBackendObject { |
| 289 | GrBackendObject obj; |
| 290 | SkAssertResult(surf->getRenderTargetHandle(&obj, access)); |
| 291 | return obj; }; |
| 292 | test_backend_handle(reporter, surf, textureAccessorFunc); |
| 293 | test_backend_handle(reporter, surf, renderTargetAccessorFunc); |
| 294 | test_texture_handle_genID(reporter, surf, textureAccessorFunc); |
| 295 | test_texture_handle_genID(reporter, surf, renderTargetAccessorFunc); |
| 296 | |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 297 | // redraw so our returned image looks as expected. |
| 298 | surf->getCanvas()->clear(color); |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 299 | return surf->newImageSnapshot(); |
| 300 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 301 | case kCodec_ImageType: { |
| 302 | SkBitmap bitmap; |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 303 | bitmap.installPixels(info, addr, rowBytes); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 304 | SkAutoTUnref<SkData> src( |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 305 | SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100)); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 306 | return SkImage::NewFromEncoded(src); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | SkASSERT(false); |
| 310 | return NULL; |
| 311 | } |
| 312 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 313 | static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) { |
| 314 | sk_memset32(pixels, color, count); |
| 315 | } |
| 316 | static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) { |
| 317 | for (int i = 0; i < count; ++i) { |
| 318 | if (pixels[i] != expected) { |
| 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image, |
| 326 | SkPMColor expected) { |
| 327 | const SkPMColor notExpected = ~expected; |
| 328 | |
| 329 | const int w = 2, h = 2; |
| 330 | const size_t rowBytes = w * sizeof(SkPMColor); |
| 331 | SkPMColor pixels[w*h]; |
| 332 | |
| 333 | SkImageInfo info; |
| 334 | |
| 335 | info = SkImageInfo::MakeUnknown(w, h); |
| 336 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 337 | |
| 338 | // out-of-bounds should fail |
| 339 | info = SkImageInfo::MakeN32Premul(w, h); |
| 340 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0)); |
| 341 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h)); |
| 342 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0)); |
| 343 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height())); |
| 344 | |
| 345 | // top-left should succeed |
| 346 | set_pixels(pixels, w*h, notExpected); |
| 347 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 348 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 349 | |
| 350 | // bottom-right should succeed |
| 351 | set_pixels(pixels, w*h, notExpected); |
| 352 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 353 | image->width() - w, image->height() - h)); |
| 354 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 355 | |
| 356 | // partial top-left should succeed |
| 357 | set_pixels(pixels, w*h, notExpected); |
| 358 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1)); |
| 359 | REPORTER_ASSERT(reporter, pixels[3] == expected); |
| 360 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected)); |
| 361 | |
| 362 | // partial bottom-right should succeed |
| 363 | set_pixels(pixels, w*h, notExpected); |
| 364 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 365 | image->width() - 1, image->height() - 1)); |
| 366 | REPORTER_ASSERT(reporter, pixels[0] == expected); |
| 367 | REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected)); |
| 368 | } |
| 369 | |
fmalita | ea561bf | 2015-07-09 17:10:31 -0700 | [diff] [blame] | 370 | static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image, |
| 371 | const SkBitmap& bitmap, SkImage::LegacyBitmapMode mode) { |
| 372 | REPORTER_ASSERT(reporter, image->width() == bitmap.width()); |
| 373 | REPORTER_ASSERT(reporter, image->height() == bitmap.height()); |
| 374 | REPORTER_ASSERT(reporter, image->isOpaque() == bitmap.isOpaque()); |
| 375 | |
| 376 | if (SkImage::kRO_LegacyBitmapMode == mode) { |
| 377 | REPORTER_ASSERT(reporter, bitmap.isImmutable()); |
| 378 | } |
| 379 | |
| 380 | SkAutoLockPixels alp(bitmap); |
| 381 | REPORTER_ASSERT(reporter, bitmap.getPixels()); |
| 382 | |
| 383 | const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType()); |
| 384 | SkPMColor imageColor; |
| 385 | REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0)); |
| 386 | REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0)); |
| 387 | } |
| 388 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 389 | static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) { |
| 390 | const SkImage::LegacyBitmapMode modes[] = { |
| 391 | SkImage::kRO_LegacyBitmapMode, |
| 392 | SkImage::kRW_LegacyBitmapMode, |
| 393 | }; |
| 394 | for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) { |
| 395 | SkBitmap bitmap; |
| 396 | REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, modes[i])); |
fmalita | ea561bf | 2015-07-09 17:10:31 -0700 | [diff] [blame] | 397 | check_legacy_bitmap(reporter, image, bitmap, modes[i]); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 398 | |
fmalita | ea561bf | 2015-07-09 17:10:31 -0700 | [diff] [blame] | 399 | // Test subsetting to exercise the rowBytes logic. |
| 400 | SkBitmap tmp; |
| 401 | REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2, |
| 402 | image->height() / 2))); |
| 403 | SkAutoTUnref<SkImage> subsetImage(SkImage::NewFromBitmap(tmp)); |
| 404 | REPORTER_ASSERT(reporter, subsetImage); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 405 | |
fmalita | ea561bf | 2015-07-09 17:10:31 -0700 | [diff] [blame] | 406 | SkBitmap subsetBitmap; |
| 407 | REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, modes[i])); |
| 408 | check_legacy_bitmap(reporter, subsetImage, subsetBitmap, modes[i]); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 412 | static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* factory) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 413 | static const struct { |
| 414 | ImageType fType; |
| 415 | bool fPeekShouldSucceed; |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 416 | const char* fName; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 417 | } gRec[] = { |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 418 | { kRasterCopy_ImageType, true, "RasterCopy" }, |
| 419 | { kRasterData_ImageType, true, "RasterData" }, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 420 | { kRasterProc_ImageType, true, "RasterProc" }, |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 421 | { kGpu_ImageType, false, "Gpu" }, |
| 422 | { kCodec_ImageType, false, "Codec" }, |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 423 | }; |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 424 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 425 | const SkColor color = SK_ColorRED; |
| 426 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 427 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 428 | GrContext* ctx = NULL; |
| 429 | #if SK_SUPPORT_GPU |
| 430 | ctx = factory->get(GrContextFactory::kNative_GLContextType); |
senorblanco | c8e9340 | 2015-04-21 07:20:36 -0700 | [diff] [blame] | 431 | if (NULL == ctx) { |
senorblanco | 84bfd39 | 2015-04-21 06:59:17 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 434 | #endif |
| 435 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 436 | ReleaseDataContext releaseCtx; |
| 437 | releaseCtx.fReporter = reporter; |
| 438 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 439 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 440 | SkImageInfo info; |
| 441 | size_t rowBytes; |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 442 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 443 | releaseCtx.fData = NULL; |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 444 | SkAutoTUnref<SkImage> image(create_image(reporter, gRec[i].fType, ctx, color, &releaseCtx)); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 445 | if (!image.get()) { |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 446 | SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 447 | continue; // gpu may not be enabled |
| 448 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 449 | if (kRasterProc_ImageType == gRec[i].fType) { |
| 450 | REPORTER_ASSERT(reporter, NULL != releaseCtx.fData); // we are tracking the data |
| 451 | } else { |
| 452 | REPORTER_ASSERT(reporter, NULL == releaseCtx.fData); // we ignored the context |
| 453 | } |
| 454 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 455 | test_legacy_bitmap(reporter, image); |
| 456 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 457 | const void* addr = image->peekPixels(&info, &rowBytes); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 458 | bool success = SkToBool(addr); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 459 | REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
| 460 | if (success) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 461 | REPORTER_ASSERT(reporter, 10 == info.width()); |
| 462 | REPORTER_ASSERT(reporter, 10 == info.height()); |
| 463 | REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType()); |
| 464 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() || |
| 465 | kOpaque_SkAlphaType == info.alphaType()); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 466 | REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); |
| 467 | REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
| 468 | } |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 469 | |
| 470 | test_image_readpixels(reporter, image, pmcolor); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 471 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 472 | REPORTER_ASSERT(reporter, NULL == releaseCtx.fData); // we released the data |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 473 | } |
| 474 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 475 | static void test_canvaspeek(skiatest::Reporter* reporter, |
| 476 | GrContextFactory* factory) { |
| 477 | static const struct { |
| 478 | SurfaceType fType; |
| 479 | bool fPeekShouldSucceed; |
| 480 | } gRec[] = { |
| 481 | { kRaster_SurfaceType, true }, |
| 482 | { kRasterDirect_SurfaceType, true }, |
| 483 | #if SK_SUPPORT_GPU |
| 484 | { kGpu_SurfaceType, false }, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 485 | { kGpuScratch_SurfaceType, false }, |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 486 | #endif |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | const SkColor color = SK_ColorRED; |
| 490 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 491 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 492 | int cnt; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 493 | #if SK_SUPPORT_GPU |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 494 | cnt = GrContextFactory::kGLContextTypeCnt; |
| 495 | #else |
| 496 | cnt = 1; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 497 | #endif |
| 498 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 499 | for (int i= 0; i < cnt; ++i) { |
| 500 | GrContext* context = NULL; |
| 501 | #if SK_SUPPORT_GPU |
| 502 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| 503 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 504 | continue; |
| 505 | } |
| 506 | context = factory->get(glCtxType); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 507 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 508 | if (NULL == context) { |
| 509 | continue; |
| 510 | } |
| 511 | #endif |
| 512 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 513 | SkImageInfo info, requestInfo; |
| 514 | size_t rowBytes; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 515 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 516 | SkAutoTUnref<SkSurface> surface(create_surface(gRec[i].fType, context, |
| 517 | kPremul_SkAlphaType, &requestInfo)); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 518 | surface->getCanvas()->clear(color); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 519 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 520 | const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 521 | bool success = SkToBool(addr); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 522 | REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 523 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 524 | SkImageInfo info2; |
| 525 | size_t rb2; |
| 526 | const void* addr2 = surface->peekPixels(&info2, &rb2); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 527 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 528 | if (success) { |
| 529 | REPORTER_ASSERT(reporter, requestInfo == info); |
| 530 | REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes); |
| 531 | REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
| 532 | |
| 533 | REPORTER_ASSERT(reporter, addr2 == addr); |
| 534 | REPORTER_ASSERT(reporter, info2 == info); |
| 535 | REPORTER_ASSERT(reporter, rb2 == rowBytes); |
| 536 | } else { |
| 537 | REPORTER_ASSERT(reporter, NULL == addr2); |
| 538 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 543 | // For compatibility with clients that still call accessBitmap(), we need to ensure that we bump |
| 544 | // the bitmap's genID when we draw to it, else they won't know it has new values. When they are |
| 545 | // exclusively using surface/image, and we can hide accessBitmap from device, we can remove this |
| 546 | // test. |
| 547 | static void test_accessPixels(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 548 | static const struct { |
| 549 | SurfaceType fType; |
| 550 | bool fPeekShouldSucceed; |
| 551 | } gRec[] = { |
| 552 | { kRaster_SurfaceType, true }, |
| 553 | { kRasterDirect_SurfaceType, true }, |
| 554 | #if SK_SUPPORT_GPU |
| 555 | { kGpu_SurfaceType, false }, |
| 556 | { kGpuScratch_SurfaceType, false }, |
| 557 | #endif |
| 558 | }; |
| 559 | |
| 560 | int cnt; |
| 561 | #if SK_SUPPORT_GPU |
| 562 | cnt = GrContextFactory::kGLContextTypeCnt; |
| 563 | #else |
| 564 | cnt = 1; |
| 565 | #endif |
| 566 | |
| 567 | for (int i= 0; i < cnt; ++i) { |
| 568 | GrContext* context = NULL; |
| 569 | #if SK_SUPPORT_GPU |
| 570 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| 571 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 572 | continue; |
| 573 | } |
| 574 | context = factory->get(glCtxType); |
| 575 | |
| 576 | if (NULL == context) { |
| 577 | continue; |
| 578 | } |
| 579 | #endif |
| 580 | for (size_t j = 0; j < SK_ARRAY_COUNT(gRec); ++j) { |
| 581 | SkImageInfo info, requestInfo; |
| 582 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 583 | SkAutoTUnref<SkSurface> surface(create_surface(gRec[j].fType, context, |
| 584 | kPremul_SkAlphaType, &requestInfo)); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 585 | SkCanvas* canvas = surface->getCanvas(); |
| 586 | canvas->clear(0); |
| 587 | |
| 588 | SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing(); |
| 589 | SkBitmap bm = device->accessBitmap(false); |
| 590 | uint32_t genID0 = bm.getGenerationID(); |
| 591 | // Now we draw something, which needs to "dirty" the genID (sorta like copy-on-write) |
| 592 | canvas->drawColor(SK_ColorBLUE); |
| 593 | // Now check that we get a different genID |
| 594 | uint32_t genID1 = bm.getGenerationID(); |
| 595 | REPORTER_ASSERT(reporter, genID0 != genID1); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 600 | static void test_snap_alphatype(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 601 | GrContext* context = NULL; |
| 602 | #if SK_SUPPORT_GPU |
| 603 | context = factory->get(GrContextFactory::kNative_GLContextType); |
| 604 | if (NULL == context) { |
| 605 | return; |
| 606 | } |
| 607 | #endif |
| 608 | for (int opaque = 0; opaque < 2; ++opaque) { |
| 609 | SkAlphaType atype = SkToBool(opaque) ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 610 | for (int st = 0; st < kSurfaceTypeCnt; ++st) { |
| 611 | SurfaceType stype = (SurfaceType)st; |
| 612 | SkAutoTUnref<SkSurface> surface(create_surface(stype, context, atype)); |
| 613 | REPORTER_ASSERT(reporter, surface); |
| 614 | if (surface) { |
| 615 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 616 | REPORTER_ASSERT(reporter, image); |
| 617 | if (image) { |
| 618 | REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(opaque)); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 625 | static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType, |
| 626 | GrContext* context) { |
| 627 | // Verify that the right canvas commands trigger a copy on write |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 628 | SkSurface* surface = create_surface(surfaceType, context); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 629 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 630 | SkCanvas* canvas = surface->getCanvas(); |
| 631 | |
| 632 | const SkRect testRect = |
| 633 | SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 634 | SkIntToScalar(4), SkIntToScalar(5)); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 635 | SkPath testPath; |
| 636 | testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
| 637 | SkIntToScalar(2), SkIntToScalar(1))); |
| 638 | |
| 639 | const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
| 640 | |
| 641 | SkRegion testRegion; |
| 642 | testRegion.setRect(testIRect); |
| 643 | |
| 644 | |
| 645 | const SkColor testColor = 0x01020304; |
| 646 | const SkPaint testPaint; |
| 647 | const SkPoint testPoints[3] = { |
| 648 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 649 | {SkIntToScalar(2), SkIntToScalar(1)}, |
| 650 | {SkIntToScalar(0), SkIntToScalar(2)} |
| 651 | }; |
| 652 | const size_t testPointCount = 3; |
| 653 | |
| 654 | SkBitmap testBitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 655 | testBitmap.allocN32Pixels(10, 10); |
robertphillips@google.com | d1ce77d | 2013-10-09 12:51:09 +0000 | [diff] [blame] | 656 | testBitmap.eraseColor(0); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 657 | |
| 658 | SkRRect testRRect; |
| 659 | testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); |
| 660 | |
| 661 | SkString testText("Hello World"); |
| 662 | const SkPoint testPoints2[] = { |
| 663 | { SkIntToScalar(0), SkIntToScalar(1) }, |
| 664 | { SkIntToScalar(1), SkIntToScalar(1) }, |
| 665 | { SkIntToScalar(2), SkIntToScalar(1) }, |
| 666 | { SkIntToScalar(3), SkIntToScalar(1) }, |
| 667 | { SkIntToScalar(4), SkIntToScalar(1) }, |
| 668 | { SkIntToScalar(5), SkIntToScalar(1) }, |
| 669 | { SkIntToScalar(6), SkIntToScalar(1) }, |
| 670 | { SkIntToScalar(7), SkIntToScalar(1) }, |
| 671 | { SkIntToScalar(8), SkIntToScalar(1) }, |
| 672 | { SkIntToScalar(9), SkIntToScalar(1) }, |
| 673 | { SkIntToScalar(10), SkIntToScalar(1) }, |
| 674 | }; |
| 675 | |
| 676 | #define EXPECT_COPY_ON_WRITE(command) \ |
| 677 | { \ |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 678 | SkImage* imageBefore = surface->newImageSnapshot(); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 679 | SkAutoTUnref<SkImage> aur_before(imageBefore); \ |
| 680 | canvas-> command ; \ |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 681 | SkImage* imageAfter = surface->newImageSnapshot(); \ |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 682 | SkAutoTUnref<SkImage> aur_after(imageAfter); \ |
| 683 | REPORTER_ASSERT(reporter, imageBefore != imageAfter); \ |
| 684 | } |
| 685 | |
| 686 | EXPECT_COPY_ON_WRITE(clear(testColor)) |
| 687 | EXPECT_COPY_ON_WRITE(drawPaint(testPaint)) |
| 688 | EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ |
| 689 | testPaint)) |
| 690 | EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint)) |
| 691 | EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint)) |
| 692 | EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint)) |
| 693 | EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
| 694 | EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 695 | EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect)) |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 696 | EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL)) |
| 697 | EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL)) |
| 698 | EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint)) |
| 699 | EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \ |
| 700 | testPaint)) |
| 701 | EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \ |
| 702 | testPaint)) |
| 703 | } |
| 704 | |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 705 | static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter, |
| 706 | SurfaceType surfaceType, |
| 707 | GrContext* context) { |
| 708 | // This test succeeds by not triggering an assertion. |
| 709 | // The test verifies that the surface remains writable (usable) after |
| 710 | // acquiring and releasing a snapshot without triggering a copy on write. |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 711 | SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context)); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 712 | SkCanvas* canvas = surface->getCanvas(); |
| 713 | canvas->clear(1); |
junov@chromium.org | 5ee449a | 2013-04-12 20:20:50 +0000 | [diff] [blame] | 714 | surface->newImageSnapshot()->unref(); // Create and destroy SkImage |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 715 | canvas->clear(2); // Must not assert internally |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 716 | } |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 717 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 718 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 719 | static void Test_crbug263329(skiatest::Reporter* reporter, |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 720 | SurfaceType surfaceType, |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 721 | GrContext* context) { |
| 722 | // This is a regression test for crbug.com/263329 |
| 723 | // Bug was caused by onCopyOnWrite releasing the old surface texture |
| 724 | // back to the scratch texture pool even though the texture is used |
| 725 | // by and active SkImage_Gpu. |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 726 | SkAutoTUnref<SkSurface> surface1(create_surface(surfaceType, context)); |
| 727 | SkAutoTUnref<SkSurface> surface2(create_surface(surfaceType, context)); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 728 | SkCanvas* canvas1 = surface1->getCanvas(); |
| 729 | SkCanvas* canvas2 = surface2->getCanvas(); |
| 730 | canvas1->clear(1); |
| 731 | SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); |
| 732 | // Trigger copy on write, new backing is a scratch texture |
| 733 | canvas1->clear(2); |
| 734 | SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); |
| 735 | // Trigger copy on write, old backing should not be returned to scratch |
| 736 | // pool because it is held by image2 |
| 737 | canvas1->clear(3); |
| 738 | |
| 739 | canvas2->clear(4); |
| 740 | SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); |
| 741 | // Trigger copy on write on surface2. The new backing store should not |
| 742 | // be recycling a texture that is held by an existing image. |
| 743 | canvas2->clear(5); |
| 744 | SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 745 | REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getTexture()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 746 | // The following assertion checks crbug.com/263329 |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 747 | REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getTexture()); |
| 748 | REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getTexture()); |
| 749 | REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getTexture()); |
| 750 | REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getTexture()); |
| 751 | REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getTexture()); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 752 | } |
| 753 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 754 | static void TestGetTexture(skiatest::Reporter* reporter, |
| 755 | SurfaceType surfaceType, |
| 756 | GrContext* context) { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 757 | SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context)); |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 758 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 759 | GrTexture* texture = as_IB(image)->getTexture(); |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 760 | if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceType) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 761 | REPORTER_ASSERT(reporter, texture); |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 762 | REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); |
| 763 | } else { |
| 764 | REPORTER_ASSERT(reporter, NULL == texture); |
| 765 | } |
| 766 | surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 767 | REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture); |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 768 | } |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 769 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 770 | #include "GrGpuResourcePriv.h" |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 771 | #include "SkGpuDevice.h" |
| 772 | #include "SkImage_Gpu.h" |
| 773 | #include "SkSurface_Gpu.h" |
| 774 | |
| 775 | SkSurface::Budgeted is_budgeted(SkSurface* surf) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 776 | return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePriv().isBudgeted() ? |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 777 | SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
| 778 | } |
| 779 | |
| 780 | SkSurface::Budgeted is_budgeted(SkImage* image) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 781 | return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ? |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 782 | SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
| 783 | } |
| 784 | |
| 785 | static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context) { |
| 786 | SkImageInfo info = SkImageInfo::MakeN32Premul(8,8); |
| 787 | for (int i = 0; i < 2; ++i) { |
| 788 | SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
| 789 | for (int j = 0; j < 2; ++j) { |
| 790 | SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
| 791 | SkAutoTUnref<SkSurface> |
| 792 | surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0)); |
| 793 | SkASSERT(surface); |
| 794 | REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
| 795 | |
mtklein | 31ff298 | 2015-01-24 11:27:27 -0800 | [diff] [blame] | 796 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted)); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 797 | |
| 798 | // Initially the image shares a texture with the surface, and the surface decides |
| 799 | // whether it is budgeted or not. |
| 800 | REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
| 801 | REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image)); |
| 802 | |
| 803 | // Now trigger copy-on-write |
| 804 | surface->getCanvas()->clear(SK_ColorBLUE); |
| 805 | |
| 806 | // They don't share a texture anymore. They should each have made their own budget |
| 807 | // decision. |
| 808 | REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
| 809 | REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image)); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 814 | #endif |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 815 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 816 | static void TestSurfaceNoCanvas(skiatest::Reporter* reporter, |
| 817 | SurfaceType surfaceType, |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 818 | GrContext* context, |
| 819 | SkSurface::ContentChangeMode mode) { |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 820 | // Verifies the robustness of SkSurface for handling use cases where calls |
| 821 | // are made before a canvas is created. |
| 822 | { |
| 823 | // Test passes by not asserting |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 824 | SkSurface* surface = create_surface(surfaceType, context); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 825 | SkAutoTUnref<SkSurface> aur_surface(surface); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 826 | surface->notifyContentWillChange(mode); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 827 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 828 | } |
| 829 | { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 830 | SkSurface* surface = create_surface(surfaceType, context); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 831 | SkAutoTUnref<SkSurface> aur_surface(surface); |
| 832 | SkImage* image1 = surface->newImageSnapshot(); |
| 833 | SkAutoTUnref<SkImage> aur_image1(image1); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 834 | SkDEBUGCODE(image1->validate();) |
| 835 | SkDEBUGCODE(surface->validate();) |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 836 | surface->notifyContentWillChange(mode); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 837 | SkDEBUGCODE(image1->validate();) |
| 838 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 839 | SkImage* image2 = surface->newImageSnapshot(); |
| 840 | SkAutoTUnref<SkImage> aur_image2(image2); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 841 | SkDEBUGCODE(image2->validate();) |
| 842 | SkDEBUGCODE(surface->validate();) |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 843 | REPORTER_ASSERT(reporter, image1 != image2); |
| 844 | } |
skia.committer@gmail.com | 45fb8b6 | 2013-04-17 07:00:56 +0000 | [diff] [blame] | 845 | |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 846 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 847 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 848 | DEF_GPUTEST(Surface, reporter, factory) { |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 849 | test_image(reporter); |
| 850 | |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 851 | TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 852 | TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 853 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode); |
| 854 | TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 855 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 856 | test_empty_image(reporter); |
| 857 | test_empty_surface(reporter, NULL); |
| 858 | |
reed | 67f2eb4 | 2014-12-10 06:54:06 -0800 | [diff] [blame] | 859 | test_imagepeek(reporter, factory); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 860 | test_canvaspeek(reporter, factory); |
| 861 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 862 | test_accessPixels(reporter, factory); |
| 863 | |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 864 | test_snap_alphatype(reporter, factory); |
| 865 | |
junov@chromium.org | b516a41 | 2013-05-01 22:49:59 +0000 | [diff] [blame] | 866 | #if SK_SUPPORT_GPU |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 867 | TestGetTexture(reporter, kRaster_SurfaceType, NULL); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 868 | if (factory) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 869 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 870 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| 871 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 872 | continue; |
| 873 | } |
| 874 | GrContext* context = factory->get(glCtxType); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 875 | if (context) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 876 | Test_crbug263329(reporter, kGpu_SurfaceType, context); |
| 877 | Test_crbug263329(reporter, kGpuScratch_SurfaceType, context); |
| 878 | TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); |
| 879 | TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, context); |
| 880 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); |
| 881 | TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_SurfaceType, context); |
| 882 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
| 883 | TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); |
| 884 | TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
| 885 | TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); |
| 886 | TestGetTexture(reporter, kGpu_SurfaceType, context); |
| 887 | TestGetTexture(reporter, kGpuScratch_SurfaceType, context); |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 888 | test_empty_surface(reporter, context); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 889 | test_surface_budget(reporter, context); |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 890 | test_wrapped_texture_surface(reporter, context); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 891 | } |
robertphillips@google.com | 3bddb38 | 2013-11-12 13:51:03 +0000 | [diff] [blame] | 892 | } |
junov@chromium.org | af05835 | 2013-04-03 15:03:26 +0000 | [diff] [blame] | 893 | } |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 894 | #endif |
| 895 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 896 | |
| 897 | #if SK_SUPPORT_GPU |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 898 | |
| 899 | struct ReleaseTextureContext { |
| 900 | ReleaseTextureContext(skiatest::Reporter* reporter) { |
| 901 | fReporter = reporter; |
| 902 | fIsReleased = false; |
| 903 | } |
| 904 | |
| 905 | skiatest::Reporter* fReporter; |
| 906 | bool fIsReleased; |
| 907 | |
| 908 | void doRelease() { |
| 909 | REPORTER_ASSERT(fReporter, false == fIsReleased); |
| 910 | fIsReleased = true; |
| 911 | } |
| 912 | |
| 913 | static void ReleaseProc(void* context) { |
| 914 | ((ReleaseTextureContext*)context)->doRelease(); |
| 915 | } |
| 916 | }; |
| 917 | |
| 918 | static SkImage* make_desc_image(GrContext* ctx, int w, int h, GrBackendObject texID, |
| 919 | ReleaseTextureContext* releaseContext) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 920 | GrBackendTextureDesc desc; |
| 921 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 922 | // need to be a rendertarget for now... |
| 923 | desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 924 | desc.fWidth = w; |
| 925 | desc.fHeight = h; |
| 926 | desc.fSampleCnt = 0; |
| 927 | desc.fTextureHandle = texID; |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 928 | return releaseContext |
| 929 | ? SkImage::NewFromTexture(ctx, desc, kPremul_SkAlphaType, |
| 930 | ReleaseTextureContext::ReleaseProc, releaseContext) |
| 931 | : SkImage::NewFromTextureCopy(ctx, desc, kPremul_SkAlphaType); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static void test_image_color(skiatest::Reporter* reporter, SkImage* image, SkPMColor expected) { |
| 935 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 936 | SkPMColor pixel; |
| 937 | REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0, 0)); |
| 938 | REPORTER_ASSERT(reporter, pixel == expected); |
| 939 | } |
| 940 | |
| 941 | DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) { |
| 942 | GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType); |
| 943 | if (!ctx) { |
| 944 | REPORTER_ASSERT(reporter, false); |
| 945 | return; |
| 946 | } |
| 947 | GrTextureProvider* provider = ctx->textureProvider(); |
| 948 | |
| 949 | const int w = 10; |
| 950 | const int h = 10; |
| 951 | SkPMColor storage[w * h]; |
| 952 | const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED); |
| 953 | sk_memset32(storage, expected0, w * h); |
| 954 | |
| 955 | GrSurfaceDesc desc; |
bsalomon | bea0150 | 2015-07-16 10:00:28 -0700 | [diff] [blame] | 956 | desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget for readpixels(); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 957 | desc.fOrigin = kDefault_GrSurfaceOrigin; |
| 958 | desc.fWidth = w; |
| 959 | desc.fHeight = h; |
| 960 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 961 | desc.fSampleCnt = 0; |
| 962 | |
| 963 | SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w * 4)); |
| 964 | if (!tex) { |
| 965 | REPORTER_ASSERT(reporter, false); |
| 966 | return; |
| 967 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 968 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 969 | GrBackendObject srcTex = tex->getTextureHandle(); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 970 | ReleaseTextureContext releaseCtx(reporter); |
| 971 | |
| 972 | SkAutoTUnref<SkImage> refImg(make_desc_image(ctx, w, h, srcTex, &releaseCtx)); |
| 973 | SkAutoTUnref<SkImage> cpyImg(make_desc_image(ctx, w, h, srcTex, NULL)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 974 | |
| 975 | test_image_color(reporter, refImg, expected0); |
| 976 | test_image_color(reporter, cpyImg, expected0); |
| 977 | |
| 978 | // Now lets jam new colors into our "external" texture, and see if the images notice |
| 979 | const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE); |
| 980 | sk_memset32(storage, expected1, w * h); |
| 981 | tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kFlushWrites_PixelOp); |
| 982 | |
bsalomon | a449779 | 2015-07-23 12:22:19 -0700 | [diff] [blame] | 983 | // The cpy'd one should still see the old color |
| 984 | #if 0 |
| 985 | // There is no guarantee that refImg sees the new color. We are free to have made a copy. Our |
| 986 | // write pixels call violated the contract with refImg and refImg is now undefined. |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 987 | test_image_color(reporter, refImg, expected1); |
bsalomon | a449779 | 2015-07-23 12:22:19 -0700 | [diff] [blame] | 988 | #endif |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 989 | test_image_color(reporter, cpyImg, expected0); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 990 | |
| 991 | // Now exercise the release proc |
| 992 | REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased); |
| 993 | refImg.reset(NULL); // force a release of the image |
| 994 | REPORTER_ASSERT(reporter, releaseCtx.fIsReleased); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 995 | } |
| 996 | #endif |