blob: 3fe498508b94bf53edd3340d4ccf845e5e4b82f7 [file] [log] [blame]
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +00008#include "SkCanvas.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
Brian Salomon5fba7ad2018-03-22 10:01:16 -040010#include "SkImageInfoPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000011#include "SkMathPriv.h"
reed4af35f32014-06-27 17:47:49 -070012#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000014#include "sk_tool_utils.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000015
Brian Salomond17f6582017-07-19 18:28:58 -040016#include "GrBackendSurface.h"
kkinnunen15302832015-12-01 04:35:26 -080017#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050018#include "GrContextPriv.h"
Brian Osman33910292017-04-18 14:38:53 -040019#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050020#include "GrProxyProvider.h"
Brian Salomond17f6582017-07-19 18:28:58 -040021#include "GrTest.h"
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000022
kkinnunen15302832015-12-01 04:35:26 -080023#include <initializer_list>
24
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000025static const int DEV_W = 100, DEV_H = 100;
26static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000027static const U8CPU DEV_PAD = 0xee;
28
bsalomonf0674512015-07-28 13:26:15 -070029static SkPMColor get_canvas_color(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000030 SkASSERT(x >= 0 && x < DEV_W);
31 SkASSERT(y >= 0 && y < DEV_H);
32
33 U8CPU r = x;
34 U8CPU g = y;
35 U8CPU b = 0xc;
36
bsalomon@google.com31648eb2011-11-23 15:01:08 +000037 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000038 switch ((x+y) % 5) {
39 case 0:
40 a = 0xff;
41 break;
42 case 1:
43 a = 0x80;
44 break;
45 case 2:
46 a = 0xCC;
47 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000048 case 3:
49 a = 0x00;
50 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000051 case 4:
52 a = 0x01;
53 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000054 }
55 return SkPremultiplyARGBInline(a, r, g, b);
56}
57
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000058// assumes any premu/.unpremul has been applied
bsalomonf0674512015-07-28 13:26:15 -070059static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000060 uint32_t r32;
61 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
reed@google.com7111d462014-03-25 16:20:24 +000062 switch (ct) {
63 case kBGRA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000064 result[0] = b;
65 result[1] = g;
66 result[2] = r;
67 result[3] = a;
68 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040069 case kRGBA_8888_SkColorType: // fallthrough
70 case kRGB_888x_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000071 result[0] = r;
72 result[1] = g;
73 result[2] = b;
74 result[3] = a;
75 break;
76 default:
77 SkASSERT(0);
78 return 0;
79 }
80 return r32;
81}
82
bsalomonf0674512015-07-28 13:26:15 -070083static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000084 int n = y * w + x;
85 U8CPU b = n & 0xff;
86 U8CPU g = (n >> 8) & 0xff;
87 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000088 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000089 switch ((x+y) % 5) {
90 case 4:
91 a = 0xff;
92 break;
93 case 3:
94 a = 0x80;
95 break;
96 case 2:
97 a = 0xCC;
98 break;
99 case 1:
100 a = 0x01;
101 break;
102 case 0:
103 a = 0x00;
104 break;
105 }
reed@google.com7111d462014-03-25 16:20:24 +0000106 if (kPremul_SkAlphaType == at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000107 r = SkMulDiv255Ceiling(r, a);
108 g = SkMulDiv255Ceiling(g, a);
109 b = SkMulDiv255Ceiling(b, a);
110 }
bsalomonf0674512015-07-28 13:26:15 -0700111 return pack_color_type(ct, a, r, g , b);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000112}
113
Mike Reed4c790bd2018-02-08 14:10:40 -0500114static void fill_surface(SkSurface* surface) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000115 SkBitmap bmp;
Mike Reed4c790bd2018-02-08 14:10:40 -0500116 bmp.allocN32Pixels(DEV_W, DEV_H);
117 for (int y = 0; y < DEV_H; ++y) {
118 for (int x = 0; x < DEV_W; ++x) {
119 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000120 }
121 }
Mike Reed4c790bd2018-02-08 14:10:40 -0500122 surface->writePixels(bmp, 0, 0);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000123}
124
reed@google.com7111d462014-03-25 16:20:24 +0000125/**
126 * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
127 * Thus this routine doesn't need to know the exact colortype
128 */
129static uint32_t premul(uint32_t color) {
130 unsigned a = SkGetPackedA32(color);
131 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
132 unsigned c0 = SkGetPackedR32(color);
133 unsigned c1 = SkGetPackedG32(color);
134 unsigned c2 = SkGetPackedB32(color);
135 c0 = SkMulDiv255Ceiling(c0, a);
136 c1 = SkMulDiv255Ceiling(c1, a);
137 c2 = SkMulDiv255Ceiling(c2, a);
138 return SkPackARGB32NoCheck(a, c0, c1, c2);
139}
140
141static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
142 if (kUnpremul_SkAlphaType == at) {
143 color = premul(color);
144 }
145 switch (ct) {
146 case kRGBA_8888_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400147 case kRGB_888x_SkColorType: // fallthrough
reed@google.com7111d462014-03-25 16:20:24 +0000148 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000149 break;
reed@google.com7111d462014-03-25 16:20:24 +0000150 case kBGRA_8888_SkColorType:
151 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000152 break;
153 default:
reed@google.com7111d462014-03-25 16:20:24 +0000154 SkASSERT(0);
155 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000156 }
reed@google.com7111d462014-03-25 16:20:24 +0000157 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000158}
159
bsalomonf0674512015-07-28 13:26:15 -0700160static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000161 if (!didPremulConversion) {
162 return a == b;
163 }
164 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
165 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
166 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
167 int32_t aB = SkGetPackedB32(a);
168
169 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
170 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
171 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
172 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
173
174 return aA == bA &&
175 SkAbs32(aR - bR) <= 1 &&
176 SkAbs32(aG - bG) <= 1 &&
177 SkAbs32(aB - bB) <= 1;
178}
179
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400180bool write_should_succeed(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo, bool isGPU) {
181 if (!SkImageInfoValidConversion(dstInfo, srcInfo)) {
182 return false;
183 }
184 if (!isGPU) {
185 return true;
186 }
187 // The GPU backend supports writing unpremul data to a premul dst but not vice versa.
188 if (srcInfo.alphaType() == kPremul_SkAlphaType &&
189 dstInfo.alphaType() == kUnpremul_SkAlphaType) {
190 return false;
191 }
192 if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
193 SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
194 return false;
195 }
196 // The source has no alpha value and the dst is only alpha
197 if (SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
198 SkColorTypeIsAlphaOnly(dstInfo.colorType())) {
199 return false;
200 }
201 return true;
202}
203
204static bool check_write(skiatest::Reporter* reporter, SkSurface* surf, SkAlphaType surfaceAlphaType,
205 const SkBitmap& bitmap, int writeX, int writeY) {
reed@google.com7111d462014-03-25 16:20:24 +0000206 size_t canvasRowBytes;
207 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000208
reed@google.com7111d462014-03-25 16:20:24 +0000209 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
210 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
211 // readPixels for the client.
212 SkBitmap secretDevBitmap;
Mike Reedf1942192017-07-21 14:24:29 -0400213 secretDevBitmap.allocN32Pixels(surf->width(), surf->height());
214 if (!surf->readPixels(secretDevBitmap, 0, 0)) {
Brian Salomon71d9d842016-11-03 13:42:00 -0400215 return false;
216 }
reed52d9ac62014-06-30 09:05:34 -0700217
reed@google.com7111d462014-03-25 16:20:24 +0000218 canvasRowBytes = secretDevBitmap.rowBytes();
219 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
220
halcanary96fcdcc2015-08-27 07:41:13 -0700221 if (nullptr == canvasPixels) {
reed@google.com7111d462014-03-25 16:20:24 +0000222 return false;
223 }
224
Mike Reedf1942192017-07-21 14:24:29 -0400225 if (surf->width() != DEV_W || surf->height() != DEV_H) {
reed@google.com7111d462014-03-25 16:20:24 +0000226 return false;
227 }
228
229 const SkImageInfo bmInfo = bitmap.info();
230
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000231 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000232 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000233 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000234 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000235 if (writeRect.contains(cx, cy)) {
236 int bx = cx - writeX;
237 int by = cy - writeY;
bsalomonf0674512015-07-28 13:26:15 -0700238 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
reed@google.com7111d462014-03-25 16:20:24 +0000239 bmInfo.colorType(), bmInfo.alphaType());
240 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
241 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
242 bmpColor8888);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400243 if (bmInfo.alphaType() == kOpaque_SkAlphaType ||
244 surfaceAlphaType == kOpaque_SkAlphaType) {
245 bmpPMColor |= 0xFF000000;
246 }
bsalomonf0674512015-07-28 13:26:15 -0700247 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
248 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
249 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000250 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000251 }
252 } else {
bsalomonf0674512015-07-28 13:26:15 -0700253 SkPMColor testColor = get_canvas_color(cx, cy);
254 if (canvasPixel != testColor) {
255 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
256 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000257 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000258 }
259 }
260 }
261 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000262 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000263 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
264 bool check;
265 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
266 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000267 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000268 }
269 }
270 }
reed@google.com7111d462014-03-25 16:20:24 +0000271 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000272 }
273
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000274 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000275}
276
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000277#include "SkMallocPixelRef.h"
278
279// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
280// a custom pixelRef (which also has to specify its rowBytes), so we have to be
281// sure that the two rowBytes match (and the infos match).
282//
bsalomonf0674512015-07-28 13:26:15 -0700283static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000284 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000285 return false;
286 }
Mike Reed086a4272017-07-18 10:53:11 -0400287 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
Hal Canary1b3387b2016-12-12 13:48:12 -0500288 bm->setPixelRef(std::move(pr), 0, 0);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000289 return true;
290}
291
reed52d9ac62014-06-30 09:05:34 -0700292static void free_pixels(void* pixels, void* ctx) {
293 sk_free(pixels);
294}
295
reed52d9ac62014-06-30 09:05:34 -0700296static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000297 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
298 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
bsalomonf0674512015-07-28 13:26:15 -0700299 if (!alloc_row_bytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000300 return false;
301 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302 for (int y = 0; y < h; ++y) {
303 for (int x = 0; x < w; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700304 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000305 }
306 }
307 return true;
308}
309
Mike Reed4c790bd2018-02-08 14:10:40 -0500310static void call_writepixels(SkSurface* surface) {
reed4af35f32014-06-27 17:47:49 -0700311 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
312 SkPMColor pixel = 0;
Mike Reed4c790bd2018-02-08 14:10:40 -0500313 surface->writePixels({info, &pixel, sizeof(SkPMColor)}, 0, 0);
reed4af35f32014-06-27 17:47:49 -0700314}
315
kkinnunen15302832015-12-01 04:35:26 -0800316DEF_TEST(WritePixelsSurfaceGenID, reporter) {
reed4af35f32014-06-27 17:47:49 -0700317 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -0700318 auto surface(SkSurface::MakeRaster(info));
reed4af35f32014-06-27 17:47:49 -0700319 uint32_t genID1 = surface->generationID();
Mike Reed4c790bd2018-02-08 14:10:40 -0500320 call_writepixels(surface.get());
reed4af35f32014-06-27 17:47:49 -0700321 uint32_t genID2 = surface->generationID();
322 REPORTER_ASSERT(reporter, genID1 != genID2);
323}
324
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400325static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface,
326 const SkImageInfo& surfaceInfo) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327 const SkIRect testRects[] = {
328 // entire thing
329 DEV_RECT,
330 // larger on all sides
331 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
332 // fully contained
333 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
334 // outside top left
335 SkIRect::MakeLTRB(-10, -10, -1, -1),
336 // touching top left corner
337 SkIRect::MakeLTRB(-10, -10, 0, 0),
338 // overlapping top left corner
339 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
340 // overlapping top left and top right corners
341 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
342 // touching entire top edge
343 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
344 // overlapping top right corner
345 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
346 // contained in x, overlapping top edge
347 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
348 // outside top right corner
349 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
350 // touching top right corner
351 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
352 // overlapping top left and bottom left corners
353 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
354 // touching entire left edge
355 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
356 // overlapping bottom left corner
357 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
358 // contained in y, overlapping left edge
359 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
360 // outside bottom left corner
361 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
362 // touching bottom left corner
363 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
364 // overlapping bottom left and bottom right corners
365 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
366 // touching entire left edge
367 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
368 // overlapping bottom right corner
369 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
370 // overlapping top right and bottom right corners
371 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
372 };
373
Mike Reedf1942192017-07-21 14:24:29 -0400374 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000375
kkinnunen15302832015-12-01 04:35:26 -0800376 static const struct {
377 SkColorType fColorType;
378 SkAlphaType fAlphaType;
379 } gSrcConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400380 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
381 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
382 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
383 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
384 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800385 };
386 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
387 const SkIRect& rect = testRects[r];
388 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
389 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
390 const SkColorType ct = gSrcConfigs[c].fColorType;
391 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000392
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400393 bool isGPU = SkToBool(surface->getCanvas()->getGrContext());
Mike Reed4c790bd2018-02-08 14:10:40 -0500394 fill_surface(surface);
kkinnunen15302832015-12-01 04:35:26 -0800395 SkBitmap bmp;
396 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
397 rect.height(), SkToBool(tightBmp)));
398 uint32_t idBefore = surface->generationID();
reed@google.com7111d462014-03-25 16:20:24 +0000399
kkinnunen15302832015-12-01 04:35:26 -0800400 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
Mike Reed4c790bd2018-02-08 14:10:40 -0500401 surface->writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000402
kkinnunen15302832015-12-01 04:35:26 -0800403 uint32_t idAfter = surface->generationID();
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400404 REPORTER_ASSERT(reporter, check_write(reporter, surface, surfaceInfo.alphaType(),
405 bmp, rect.fLeft, rect.fTop));
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000406
kkinnunen15302832015-12-01 04:35:26 -0800407 // we should change the genID iff pixels were actually written.
Mike Reedf1942192017-07-21 14:24:29 -0400408 SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize());
kkinnunen15302832015-12-01 04:35:26 -0800409 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
410 bmp.width(), bmp.height());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400411 bool expectSuccess = SkIRect::Intersects(canvasRect, writeRect) &&
412 write_should_succeed(surfaceInfo, bmp.info(), isGPU);
413 REPORTER_ASSERT(reporter, expectSuccess == (idBefore != idAfter));
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000414 }
415 }
416 }
417}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400418
kkinnunen15302832015-12-01 04:35:26 -0800419DEF_TEST(WritePixels, reporter) {
420 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
421 for (auto& tightRowBytes : { true, false }) {
422 const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
Mike Reedf0ffb892017-10-03 14:47:21 -0400423 const size_t size = info.computeByteSize(rowBytes);
kkinnunen15302832015-12-01 04:35:26 -0800424 void* pixels = sk_malloc_throw(size);
425 // if rowBytes isn't tight then set the padding to a known value
426 if (!tightRowBytes) {
427 memset(pixels, DEV_PAD, size);
428 }
reede8f30622016-03-23 18:59:25 -0700429 auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
430 free_pixels, nullptr));
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400431 test_write_pixels(reporter, surface.get(), info);
kkinnunen15302832015-12-01 04:35:26 -0800432 }
433}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400434
Brian Salomon3d86a192018-02-27 16:46:11 -0500435static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, int sampleCnt) {
robertphillips7e922762016-07-26 11:38:17 -0700436 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
Brian Salomon3d86a192018-02-27 16:46:11 -0500437 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
438 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context,
439 SkBudgeted::kNo, ii, sampleCnt,
440 origin, nullptr));
441 if (surface) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400442 test_write_pixels(reporter, surface.get(), ii);
Brian Salomon3d86a192018-02-27 16:46:11 -0500443 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500444 }
445}
446
447DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) {
448 test_write_pixels(reporter, ctxInfo.grContext(), 1);
449}
450
451DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsMSAA_Gpu, reporter, ctxInfo) {
452 test_write_pixels(reporter, ctxInfo.grContext(), 1);
453}
454
455static void test_write_pixels_non_texture(skiatest::Reporter* reporter, GrContext* context,
456 int sampleCnt) {
457 GrGpu* gpu = context->contextPriv().getGpu();
robertphillips7e922762016-07-26 11:38:17 -0700458
kkinnunen15302832015-12-01 04:35:26 -0800459 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Brian Salomon3d86a192018-02-27 16:46:11 -0500460 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
461 nullptr, DEV_W, DEV_H, kSkia8888_GrPixelConfig, true, GrMipMapped::kNo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400462 if (!backendTex.isValid()) {
463 continue;
464 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500465 SkColorType colorType = kN32_SkColorType;
466 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget(
467 context, backendTex, origin, sampleCnt, colorType, nullptr, nullptr));
468 if (surface) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400469 auto ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
470 test_write_pixels(reporter, surface.get(), ii);
Brian Osman33910292017-04-18 14:38:53 -0400471 }
Brian Salomon26102cb2018-03-09 09:33:19 -0500472 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Osman33910292017-04-18 14:38:53 -0400473 }
474}
475
476DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) {
Brian Salomon3d86a192018-02-27 16:46:11 -0500477 test_write_pixels_non_texture(reporter, ctxInfo.grContext(), 1);
478}
Brian Osman33910292017-04-18 14:38:53 -0400479
Brian Salomon3d86a192018-02-27 16:46:11 -0500480DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTextureMSAA_Gpu, reporter, ctxInfo) {
481 test_write_pixels_non_texture(reporter, ctxInfo.grContext(), 4);
kkinnunen15302832015-12-01 04:35:26 -0800482}
Robert Phillips7bbbf622017-10-17 07:36:59 -0400483
484static sk_sp<SkSurface> create_surf(GrContext* context, int width, int height) {
485 const SkImageInfo ii = SkImageInfo::Make(width, height,
486 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
487
488 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
489 surf->flush();
490 return surf;
491}
492
493static sk_sp<SkImage> upload(const sk_sp<SkSurface>& surf, SkColor color) {
494 const SkImageInfo smII = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
495 SkBitmap bm;
496 bm.allocPixels(smII);
497 bm.eraseColor(color);
498
Mike Reed4c790bd2018-02-08 14:10:40 -0500499 surf->writePixels(bm, 0, 0);
Robert Phillips7bbbf622017-10-17 07:36:59 -0400500
501 return surf->makeImageSnapshot();
502}
503
504// This is tests whether the first writePixels is completed before the
505// second writePixels takes effect (i.e., that writePixels correctly flushes
506// in between uses of the shared backing resource).
507DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsPendingIO, reporter, ctxInfo) {
508 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500509 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400510
511 static const int kFullSize = 62;
512 static const int kHalfSize = 31;
513
514 static const uint32_t kLeftColor = 0xFF222222;
515 static const uint32_t kRightColor = 0xFFAAAAAA;
516
517 const SkImageInfo fullII = SkImageInfo::Make(kFullSize, kFullSize,
518 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
519 const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize,
520 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
521
522 sk_sp<SkSurface> dest = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, fullII);
523
524 {
525 // Seed the resource cached with a scratch texture that will be
526 // reused by writeSurfacePixels
527 GrSurfaceDesc desc;
528 desc.fFlags = kNone_GrSurfaceFlags;
529 desc.fWidth = 32;
530 desc.fHeight = 64;
531 desc.fConfig = kRGBA_8888_GrPixelConfig;
532
Brian Salomon2a4f9832018-03-03 22:43:43 -0500533 sk_sp<GrTextureProxy> temp = proxyProvider->createProxy(
534 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500535 temp->instantiate(context->contextPriv().resourceProvider());
Robert Phillips7bbbf622017-10-17 07:36:59 -0400536 }
537
538 // Create the surfaces and flush them to ensure there is no lingering pendingIO
539 sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize);
540 sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize);
541
542 sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor);
543 dest->getCanvas()->drawImage(std::move(leftImg), 0, 0);
544
545 sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor);
546 dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0);
547
548 SkBitmap bm;
549 bm.allocPixels(fullII);
550 SkAssertResult(dest->readPixels(bm, 0, 0));
551
552 bool isCorrect = true;
553 for (int y = 0; isCorrect && y < 16; ++y) {
554 const uint32_t* sl = bm.getAddr32(0, y);
555
556 for (int x = 0; x < 16; ++x) {
557 if (kLeftColor != sl[x]) {
558 isCorrect = false;
559 break;
560 }
561 }
562 for (int x = kHalfSize; x < kHalfSize+16; ++x) {
563 if (kRightColor != sl[x]) {
564 isCorrect = false;
565 break;
566 }
567 }
568 }
569
570 REPORTER_ASSERT(reporter, isCorrect);
571}