blob: f630eec6c8d700ce2350e2abcdb913089623cb3d [file] [log] [blame]
Hal Canary87515122019-03-15 14:22:51 -04001// 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.
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003#include "tools/fiddle/examples.h"
Hal Canary87515122019-03-15 14:22:51 -04004// HASH=a7ac9c21bbabcdeeca00f72a61cd0f3e
Hal Canarya7181e7c2019-03-18 16:06:34 -04005REG_FIDDLE(Canvas_accessTopLayerPixels_b, 256, 256, false, 0) {
Hal Canary87515122019-03-15 14:22:51 -04006void 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