| 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 |  | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" | 
| Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/GrContext.h" | 
| Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrDirectContext.h" | 
|  | 16 | #include "include/gpu/GrRecordingContext.h" | 
| Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 17 |  | 
| Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | class GrRenderTargetContext; | 
|  | 19 |  | 
| Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 20 | // This test exercises Ganesh's drawing of tiled bitmaps. In particular, that the offsets and the | 
|  | 21 | // extents of the tiles don't causes gaps between tiles. | 
| Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 22 | static void draw_tile_bitmap_with_fractional_offset(GrRecordingContext* context, | 
|  | 23 | SkCanvas* canvas, | 
| Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 24 | bool vertical) { | 
| Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 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 |  | 
| Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 34 | if (auto direct = context->asDirectContext()) { | 
| Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 35 | // To trigger tiling, we also need the image to be more than 50% of the cache, so we | 
|  | 36 | // ensure the cache is sized to make that true. | 
|  | 37 | const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge; | 
|  | 38 | const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor); | 
| Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 39 |  | 
| Robert Phillips | e393901 | 2020-06-26 08:08:22 -0400 | [diff] [blame] | 40 | const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2); | 
|  | 41 | direct->setResourceCacheLimit(newMaxResourceBytes); | 
|  | 42 | } | 
| Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 43 |  | 
|  | 44 | // Construct our bitmap as either very wide or very tall | 
|  | 45 | SkBitmap bmp; | 
|  | 46 | bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge, | 
|  | 47 | vertical ? kBitmapLongEdge : kBitmapShortEdge, true); | 
|  | 48 | bmp.eraseColor(SK_ColorWHITE); | 
|  | 49 |  | 
|  | 50 | // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling | 
|  | 51 | for (int i = 0; i < 10; ++i) { | 
|  | 52 | float offset = i * 0.1f; | 
|  | 53 | if (vertical) { | 
|  | 54 | canvas->drawBitmapRect(bmp, SkRect::MakeXYWH(0.0f, (kTileSize - 50) + offset, | 
|  | 55 | 32.0f, 1124.0f), | 
|  | 56 | SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f), nullptr); | 
|  | 57 | } else { | 
|  | 58 | canvas->drawBitmapRect(bmp, SkRect::MakeXYWH((kTileSize - 50) + offset, 0.0f, | 
|  | 59 | 1124.0f, 32.0f), | 
|  | 60 | SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f), nullptr); | 
|  | 61 | } | 
|  | 62 | } | 
| Brian Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 65 | DEF_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 Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 70 | DEF_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 Osman | a950a86 | 2017-02-06 16:48:57 -0500 | [diff] [blame] | 73 | } |