• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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.
draw_tile_bitmap_with_fractional_offset(SkCanvas * canvas,bool vertical)14 static void draw_tile_bitmap_with_fractional_offset(SkCanvas* canvas, bool vertical) {
15     GrContext* context = canvas->getGrContext();
16     if (!context) {
17         skiagm::GM::DrawGpuOnlyMessage(canvas);
18         return;
19     }
20 
21     // This should match kBmpSmallTileSize in SkGpuDevice.cpp. Note that our canvas size is tuned
22     // to this constant as well.
23     const int kTileSize = 1 << 10;
24 
25     // We're going to draw a section of the bitmap that intersects 3 tiles (3x1 or 1x3).
26     // We need that to be < 50% of the total image, so our image is 7 tiles (7x1 or 1x7).
27     const int kBitmapLongEdge = 7 * kTileSize;
28     const int kBitmapShortEdge = 1 * kTileSize;
29 
30     // To trigger tiling, we also need the image to be more than 50% of the cache, so we ensure the
31     // cache is sized to make that true.
32     const int kBitmapArea = kBitmapLongEdge * kBitmapShortEdge;
33     const size_t kBitmapBytes = kBitmapArea * sizeof(SkPMColor);
34 
35     int oldMaxResources;
36     size_t oldMaxResourceBytes;
37     context->getResourceCacheLimits(&oldMaxResources, &oldMaxResourceBytes);
38 
39     const size_t newMaxResourceBytes = kBitmapBytes + (kBitmapBytes / 2);
40     context->setResourceCacheLimits(oldMaxResources, newMaxResourceBytes);
41 
42     // Construct our bitmap as either very wide or very tall
43     SkBitmap bmp;
44     bmp.allocN32Pixels(vertical ? kBitmapShortEdge : kBitmapLongEdge,
45                        vertical ? kBitmapLongEdge : kBitmapShortEdge, true);
46     bmp.eraseColor(SK_ColorWHITE);
47 
48     // Draw ten strips with varying fractional offset to catch any rasterization issues with tiling
49     for (int i = 0; i < 10; ++i) {
50         float offset = i * 0.1f;
51         if (vertical) {
52             canvas->drawBitmapRect(bmp, SkRect::MakeXYWH(0.0f, (kTileSize - 50) + offset,
53                                                          32.0f, 1124.0f),
54                                    SkRect::MakeXYWH(37.0f * i, 0.0f, 32.0f, 1124.0f), nullptr);
55         } else {
56             canvas->drawBitmapRect(bmp, SkRect::MakeXYWH((kTileSize - 50) + offset, 0.0f,
57                                                          1124.0f, 32.0f),
58                                    SkRect::MakeXYWH(0.0f, 37.0f * i, 1124.0f, 32.0f), nullptr);
59         }
60     }
61 
62     // Restore the cache
63     context->setResourceCacheLimits(oldMaxResources, oldMaxResourceBytes);
64 }
65 
66 DEF_SIMPLE_GM_BG(bitmaptiled_fractional_horizontal, canvas, 1124, 365, SK_ColorBLACK) {
67     draw_tile_bitmap_with_fractional_offset(canvas, false);
68 }
69 
70 DEF_SIMPLE_GM_BG(bitmaptiled_fractional_vertical, canvas, 365, 1124, SK_ColorBLACK) {
71     draw_tile_bitmap_with_fractional_offset(canvas, true);
72 }
73