blob: 20709950b9f88844e9c0f5405e47273250cc0dd1 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrContext.h"
Brian Osmana950a862017-02-06 16:48:57 -050015
Ben Wagner7fde8e12019-05-01 17:28:53 -040016class GrRenderTargetContext;
17
Brian Osmana950a862017-02-06 16:48:57 -050018// This test exercises Ganesh's drawing of tiled bitmaps. In particular, that the offsets and the
19// extents of the tiles don't causes gaps between tiles.
Chris Dalton3a778372019-02-07 15:23:36 -070020static void draw_tile_bitmap_with_fractional_offset(GrContext* context, SkCanvas* canvas,
21 bool vertical) {
Brian Osmana950a862017-02-06 16:48:57 -050022 // This should match kBmpSmallTileSize in SkGpuDevice.cpp. Note that our canvas size is tuned
23 // to this constant as well.
24 const int kTileSize = 1 << 10;
25
26 // We're going to draw a section of the bitmap that intersects 3 tiles (3x1 or 1x3).
27 // We need that to be < 50% of the total image, so our image is 7 tiles (7x1 or 1x7).
28 const int kBitmapLongEdge = 7 * kTileSize;
29 const int kBitmapShortEdge = 1 * kTileSize;
30
31 // To trigger tiling, we also need the image to be more than 50% of the cache, so we ensure the
32 // cache is sized to make that true.
33 const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge;
34 const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor);
35
Robert Phillipscf39f372019-09-03 10:29:20 -040036 size_t oldMaxResourceBytes = context->getResourceCacheLimit();
Brian Osmana950a862017-02-06 16:48:57 -050037
38 const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2);
Robert Phillipscf39f372019-09-03 10:29:20 -040039 context->setResourceCacheLimit(newMaxResourceBytes);
Brian Osmana950a862017-02-06 16:48:57 -050040
41 // Construct our bitmap as either very wide or very tall
42 SkBitmap bmp;
43 bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge,
44 vertical ? kBitmapLongEdge : kBitmapShortEdge, true);
45 bmp.eraseColor(SK_ColorWHITE);
46
47 // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling
48 for (int i = 0; i < 10; ++i) {
49 float offset = i * 0.1f;
50 if (vertical) {
51 canvas->drawBitmapRect(bmp, SkRect::MakeXYWH(0.0f, (kTileSize - 50) + offset,
52 32.0f, 1124.0f),
53 SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f), nullptr);
54 } else {
55 canvas->drawBitmapRect(bmp, SkRect::MakeXYWH((kTileSize - 50) + offset, 0.0f,
56 1124.0f, 32.0f),
57 SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f), nullptr);
58 }
59 }
60
61 // Restore the cache
Robert Phillipscf39f372019-09-03 10:29:20 -040062 context->setResourceCacheLimit(oldMaxResourceBytes);
Brian Osmana950a862017-02-06 16:48:57 -050063}
64
Chris Dalton3a778372019-02-07 15:23:36 -070065DEF_SIMPLE_GPU_GM_BG(
66 bitmaptiled_fractional_horizontal, context, rtc, canvas, 1124, 365, SK_ColorBLACK) {
67 draw_tile_bitmap_with_fractional_offset(context, canvas, false);
Brian Osmana950a862017-02-06 16:48:57 -050068}
69
Chris Dalton3a778372019-02-07 15:23:36 -070070DEF_SIMPLE_GPU_GM_BG(
71 bitmaptiled_fractional_vertical, context, rtc, canvas, 365, 1124, SK_ColorBLACK) {
72 draw_tile_bitmap_with_fractional_offset(context, canvas, true);
Brian Osmana950a862017-02-06 16:48:57 -050073}