blob: 40b498d80b90817845a9c012c244bd7f442055a8 [file] [log] [blame]
Brian Osmana950a862017-02-06 16:48:57 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkRect.h"
13#include "include/core/SkTypes.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040014#include "include/gpu/GrDirectContext.h"
15#include "include/gpu/GrRecordingContext.h"
Brian Osmana950a862017-02-06 16:48:57 -050016
Brian Salomoneebe7352020-12-09 16:37:04 -050017class GrSurfaceDrawContext;
Ben Wagner7fde8e12019-05-01 17:28:53 -040018
Brian Osmana950a862017-02-06 16:48:57 -050019// This test exercises Ganesh's drawing of tiled bitmaps. In particular, that the offsets and the
20// extents of the tiles don't causes gaps between tiles.
Robert Phillips95c250c2020-06-29 15:36:12 -040021static void draw_tile_bitmap_with_fractional_offset(GrRecordingContext* context,
22 SkCanvas* canvas,
Chris Dalton3a778372019-02-07 15:23:36 -070023 bool vertical) {
Brian Osmana950a862017-02-06 16:48:57 -050024 // This should match kBmpSmallTileSize in SkGpuDevice.cpp. Note that our canvas size is tuned
25 // to this constant as well.
26 const int kTileSize = 1 << 10;
27
28 // We're going to draw a section of the bitmap that intersects 3 tiles (3x1 or 1x3).
29 // We need that to be < 50% of the total image, so our image is 7 tiles (7x1 or 1x7).
30 const int kBitmapLongEdge = 7 * kTileSize;
31 const int kBitmapShortEdge = 1 * kTileSize;
32
Robert Phillipsf8f45d92020-07-01 11:11:18 -040033 if (auto direct = context->asDirectContext()) {
Robert Phillipse3939012020-06-26 08:08:22 -040034 // To trigger tiling, we also need the image to be more than 50% of the cache, so we
35 // ensure the cache is sized to make that true.
36 const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge;
37 const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor);
Brian Osmana950a862017-02-06 16:48:57 -050038
Robert Phillipse3939012020-06-26 08:08:22 -040039 const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2);
40 direct->setResourceCacheLimit(newMaxResourceBytes);
41 }
Brian Osmana950a862017-02-06 16:48:57 -050042
43 // Construct our bitmap as either very wide or very tall
44 SkBitmap bmp;
45 bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge,
46 vertical ? kBitmapLongEdge : kBitmapShortEdge, true);
47 bmp.eraseColor(SK_ColorWHITE);
48
49 // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling
50 for (int i = 0; i < 10; ++i) {
51 float offset = i * 0.1f;
52 if (vertical) {
Mike Reed607a3822021-01-24 19:49:21 -050053 canvas->drawImageRect(bmp.asImage(),
54 SkRect::MakeXYWH(0, (kTileSize - 50) + offset, 32, 1124.0f),
55 SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f),
56 SkSamplingOptions(), nullptr,
57 SkCanvas::kStrict_SrcRectConstraint);
Brian Osmana950a862017-02-06 16:48:57 -050058 } else {
Mike Reed607a3822021-01-24 19:49:21 -050059 canvas->drawImageRect(bmp.asImage(),
60 SkRect::MakeXYWH((kTileSize - 50) + offset, 0, 1124, 32),
61 SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f),
62 SkSamplingOptions(), nullptr,
63 SkCanvas::kStrict_SrcRectConstraint);
Brian Osmana950a862017-02-06 16:48:57 -050064 }
65 }
Brian Osmana950a862017-02-06 16:48:57 -050066}
67
Chris Dalton3a778372019-02-07 15:23:36 -070068DEF_SIMPLE_GPU_GM_BG(
69 bitmaptiled_fractional_horizontal, context, rtc, canvas, 1124, 365, SK_ColorBLACK) {
70 draw_tile_bitmap_with_fractional_offset(context, canvas, false);
Brian Osmana950a862017-02-06 16:48:57 -050071}
72
Chris Dalton3a778372019-02-07 15:23:36 -070073DEF_SIMPLE_GPU_GM_BG(
74 bitmaptiled_fractional_vertical, context, rtc, canvas, 365, 1124, SK_ColorBLACK) {
75 draw_tile_bitmap_with_fractional_offset(context, canvas, true);
Brian Osmana950a862017-02-06 16:48:57 -050076}