1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "gm/gm.h"
9cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
10cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
11cb93a386Sopenharmony_ci#include "include/core/SkColor.h"
12cb93a386Sopenharmony_ci#include "include/core/SkRect.h"
13cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
14cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
15cb93a386Sopenharmony_ci#include "include/gpu/GrRecordingContext.h"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci// This test exercises Ganesh's drawing of tiled bitmaps. In particular, that the offsets and the
18cb93a386Sopenharmony_ci// extents of the tiles don't causes gaps between tiles.
19cb93a386Sopenharmony_cistatic void draw_tile_bitmap_with_fractional_offset(GrRecordingContext* rContext,
20cb93a386Sopenharmony_ci                                                    SkCanvas* canvas,
21cb93a386Sopenharmony_ci                                                    bool vertical) {
22cb93a386Sopenharmony_ci    // This should match kBmpSmallTileSize in SkGpuDevice.cpp. Note that our canvas size is tuned
23cb93a386Sopenharmony_ci    // to this constant as well.
24cb93a386Sopenharmony_ci    const int kTileSize = 1 << 10;
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci    // We're going to draw a section of the bitmap that intersects 3 tiles (3x1 or 1x3).
27cb93a386Sopenharmony_ci    // We need that to be < 50% of the total image, so our image is 7 tiles (7x1 or 1x7).
28cb93a386Sopenharmony_ci    const int kBitmapLongEdge = 7 * kTileSize;
29cb93a386Sopenharmony_ci    const int kBitmapShortEdge = 1 * kTileSize;
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci    if (auto dContext = rContext->asDirectContext()) {
32cb93a386Sopenharmony_ci        // To trigger tiling, we also need the image to be more than 50% of the cache, so we
33cb93a386Sopenharmony_ci        // ensure the cache is sized to make that true.
34cb93a386Sopenharmony_ci        const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge;
35cb93a386Sopenharmony_ci        const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor);
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci        const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2);
38cb93a386Sopenharmony_ci        dContext->setResourceCacheLimit(newMaxResourceBytes);
39cb93a386Sopenharmony_ci    }
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    // Construct our bitmap as either very wide or very tall
42cb93a386Sopenharmony_ci    SkBitmap bmp;
43cb93a386Sopenharmony_ci    bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge,
44cb93a386Sopenharmony_ci                       vertical ? kBitmapLongEdge : kBitmapShortEdge, true);
45cb93a386Sopenharmony_ci    bmp.eraseColor(SK_ColorWHITE);
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci    // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling
48cb93a386Sopenharmony_ci    for (int i = 0; i < 10; ++i) {
49cb93a386Sopenharmony_ci        float offset = i * 0.1f;
50cb93a386Sopenharmony_ci        if (vertical) {
51cb93a386Sopenharmony_ci            canvas->drawImageRect(bmp.asImage(),
52cb93a386Sopenharmony_ci                                  SkRect::MakeXYWH(0, (kTileSize - 50) + offset, 32, 1124.0f),
53cb93a386Sopenharmony_ci                                  SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f),
54cb93a386Sopenharmony_ci                                  SkSamplingOptions(), nullptr,
55cb93a386Sopenharmony_ci                                  SkCanvas::kStrict_SrcRectConstraint);
56cb93a386Sopenharmony_ci        } else {
57cb93a386Sopenharmony_ci            canvas->drawImageRect(bmp.asImage(),
58cb93a386Sopenharmony_ci                                  SkRect::MakeXYWH((kTileSize - 50) + offset, 0, 1124, 32),
59cb93a386Sopenharmony_ci                                  SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f),
60cb93a386Sopenharmony_ci                                  SkSamplingOptions(), nullptr,
61cb93a386Sopenharmony_ci                                  SkCanvas::kStrict_SrcRectConstraint);
62cb93a386Sopenharmony_ci        }
63cb93a386Sopenharmony_ci    }
64cb93a386Sopenharmony_ci}
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ciDEF_SIMPLE_GPU_GM_BG(
67cb93a386Sopenharmony_ci        bitmaptiled_fractional_horizontal, rContext, canvas, 1124, 365, SK_ColorBLACK) {
68cb93a386Sopenharmony_ci    draw_tile_bitmap_with_fractional_offset(rContext, canvas, false);
69cb93a386Sopenharmony_ci}
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ciDEF_SIMPLE_GPU_GM_BG(
72cb93a386Sopenharmony_ci        bitmaptiled_fractional_vertical, rContext, canvas, 365, 1124, SK_ColorBLACK) {
73cb93a386Sopenharmony_ci    draw_tile_bitmap_with_fractional_offset(rContext, canvas, true);
74cb93a386Sopenharmony_ci}
75