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