Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 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.h" |
| 9 | |
Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | |
| 12 | // This test exercises Ganesh's drawing of tiled bitmaps. In particular, that the offsets and the |
| 13 | // extents of the tiles don't causes gaps between tiles. |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 14 | static void draw_tile_bitmap_with_fractional_offset(GrContext* context, SkCanvas* canvas, |
| 15 | bool vertical) { |
Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 16 | // This should match kBmpSmallTileSize in SkGpuDevice.cpp. Note that our canvas size is tuned |
| 17 | // to this constant as well. |
| 18 | const int kTileSize = 1 << 10; |
| 19 | |
| 20 | // We're going to draw a section of the bitmap that intersects 3 tiles (3x1 or 1x3). |
| 21 | // We need that to be < 50% of the total image, so our image is 7 tiles (7x1 or 1x7). |
| 22 | const int kBitmapLongEdge = 7 * kTileSize; |
| 23 | const int kBitmapShortEdge = 1 * kTileSize; |
| 24 | |
| 25 | // To trigger tiling, we also need the image to be more than 50% of the cache, so we ensure the |
| 26 | // cache is sized to make that true. |
| 27 | const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge; |
| 28 | const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor); |
| 29 | |
| 30 | int oldMaxResources; |
| 31 | size_t oldMaxResourceBytes; |
| 32 | context->getResourceCacheLimits(&oldMaxResources, &oldMaxResourceBytes); |
| 33 | |
| 34 | const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2); |
| 35 | context->setResourceCacheLimits(oldMaxResources, newMaxResourceBytes); |
| 36 | |
| 37 | // Construct our bitmap as either very wide or very tall |
| 38 | SkBitmap bmp; |
| 39 | bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge, |
| 40 | vertical ? kBitmapLongEdge : kBitmapShortEdge, true); |
| 41 | bmp.eraseColor(SK_ColorWHITE); |
| 42 | |
| 43 | // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling |
| 44 | for (int i = 0; i < 10; ++i) { |
| 45 | float offset = i * 0.1f; |
| 46 | if (vertical) { |
| 47 | canvas->drawBitmapRect(bmp, SkRect::MakeXYWH(0.0f, (kTileSize - 50) + offset, |
| 48 | 32.0f, 1124.0f), |
| 49 | SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f), nullptr); |
| 50 | } else { |
| 51 | canvas->drawBitmapRect(bmp, SkRect::MakeXYWH((kTileSize - 50) + offset, 0.0f, |
| 52 | 1124.0f, 32.0f), |
| 53 | SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f), nullptr); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Restore the cache |
| 58 | context->setResourceCacheLimits(oldMaxResources, oldMaxResourceBytes); |
| 59 | } |
| 60 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 61 | DEF_SIMPLE_GPU_GM_BG( |
| 62 | bitmaptiled_fractional_horizontal, context, rtc, canvas, 1124, 365, SK_ColorBLACK) { |
| 63 | draw_tile_bitmap_with_fractional_offset(context, canvas, false); |
Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 66 | DEF_SIMPLE_GPU_GM_BG( |
| 67 | bitmaptiled_fractional_vertical, context, rtc, canvas, 365, 1124, SK_ColorBLACK) { |
| 68 | draw_tile_bitmap_with_fractional_offset(context, canvas, true); |
Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 69 | } |