Debian Codegraph @

gm/bitmaptiled.cpp

raw
1 /*
2 * Copyright 2017 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
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkTiledImageUtils.h"
15 #include "include/core/SkTypes.h"
16
17 #if defined(SK_GANESH)
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "include/gpu/ganesh/GrRecordingContext.h"
20 #endif
21
22 // This test exercises Ganesh's drawing of tiled bitmaps. In particular, that the offsets and the
23 // extents of the tiles don't cause gaps between tiles.
24 static void draw_tile_bitmap_with_fractional_offset(SkCanvas* canvas, bool vertical, bool manual) {
25 // This should match kBmpSmallTileSize in SkGpuDevice.cpp. Note that our canvas size is tuned
26 // to this constant as well.
27 const int kTileSize = 1 << 10;
28
29 // We're going to draw a section of the bitmap that intersects 3 tiles (3x1 or 1x3).
30 // We need that to be < 50% of the total image, so our image is 7 tiles (7x1 or 1x7).
31 const int kBitmapLongEdge = 7 * kTileSize;
32 const int kBitmapShortEdge = 1 * kTileSize;
33
34 #if defined(SK_GANESH)
35 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
36 // To trigger tiling, we also need the image to be more than 50% of the cache, so we
37 // ensure the cache is sized to make that true.
38 const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge;
39 const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor);
40
41 const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2);
42 dContext->setResourceCacheLimit(newMaxResourceBytes);
43 }
44 #endif
45
46 // Construct our bitmap as either very wide or very tall
47 SkBitmap bmp;
48 bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge,
49 vertical ? kBitmapLongEdge : kBitmapShortEdge, true);
50 bmp.eraseColor(SK_ColorWHITE);
51
52 // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling
53 for (int i = 0; i < 10; ++i) {
54 float offset = i * 0.1f;
55
56 SkRect src = vertical ? SkRect::MakeXYWH(0, (kTileSize - 50) + offset, 32, 1124.0f)
57 : SkRect::MakeXYWH((kTileSize - 50) + offset, 0, 1124, 32);
58 SkRect dst = vertical ? SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f)
59 : SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f);
60
61 if (manual) {
62 SkTiledImageUtils::DrawImageRect(canvas, bmp.asImage(), src, dst, SkSamplingOptions(),
63 /* paint= */ nullptr,
64 SkCanvas::kStrict_SrcRectConstraint);
65 } else {
66 canvas->drawImageRect(bmp.asImage(), src, dst, SkSamplingOptions(),
67 /* paint= */ nullptr,
68 SkCanvas::kStrict_SrcRectConstraint);
69 }
70 }
71 }
72
73 // In Graphite, it is always the client's responsibility to manually do tiled image draws, so we
74 // only run the non-manual tests in Ganesh.
75 #if defined(SK_GANESH)
76 DEF_SIMPLE_GPU_GM_BG(bitmaptiled_fractional_horizontal, rContext, canvas, 1124, 365, SK_ColorBLACK) {
77 draw_tile_bitmap_with_fractional_offset(canvas, /* vertical= */ false, /* manual= */ false);
78 }
79 DEF_SIMPLE_GPU_GM_BG(bitmaptiled_fractional_vertical, rContext, canvas, 365, 1124, SK_ColorBLACK) {
80 draw_tile_bitmap_with_fractional_offset(canvas, /* vertical= */ true, /* manual= */ false);
81 }
82 #endif
83
84 DEF_SIMPLE_GM_BG(bitmaptiled_fractional_horizontal_manual, canvas, 1124, 365, SK_ColorBLACK) {
85 draw_tile_bitmap_with_fractional_offset(canvas, /* vertical= */ false, /* manual= */ true);
86 }
87 DEF_SIMPLE_GM_BG(bitmaptiled_fractional_vertical_manual, canvas, 365, 1124, SK_ColorBLACK) {
88 draw_tile_bitmap_with_fractional_offset(canvas, /* vertical= */ true, /* manual= */ true);
89 }