1// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3#include "tools/fiddle/examples.h"
4// HASH=a7ac9c21bbabcdeeca00f72a61cd0f3e
5REG_FIDDLE(Canvas_accessTopLayerPixels_b, 256, 256, false, 0) {
6void draw(SkCanvas* canvas) {
7  SkPaint paint;
8  SkFont font(nullptr, 100);
9  canvas->drawString("ABC", 20, 160, font, paint);
10  SkRect layerBounds = SkRect::MakeXYWH(32, 32, 192, 192);
11  canvas->saveLayerAlpha(&layerBounds, 128);
12  canvas->clear(SK_ColorWHITE);
13  canvas->drawString("DEF", 20, 160, font, paint);
14  SkImageInfo imageInfo;
15  size_t rowBytes;
16  SkIPoint origin;
17  uint32_t* access = (uint32_t*) canvas->accessTopLayerPixels(&imageInfo, &rowBytes, &origin);
18  if (access) {
19    int h = imageInfo.height();
20    int v = imageInfo.width();
21    int rowWords = rowBytes / sizeof(uint32_t);
22    for (int y = 0; y < h; ++y) {
23        int newY = (y - h / 2) * 2 + h / 2;
24        if (newY < 0 || newY >= h) {
25            continue;
26        }
27        for (int x = 0; x < v; ++x) {
28            int newX = (x - v / 2) * 2 + v / 2;
29            if (newX < 0 || newX >= v) {
30                continue;
31            }
32            if (access[y * rowWords + x] == SK_ColorBLACK) {
33                access[newY * rowWords + newX] = SK_ColorGRAY;
34            }
35        }
36    }
37  }
38  canvas->restore();
39}
40}  // END FIDDLE
41