1cb93a386Sopenharmony_ci// Copyright 2020 Google LLC. 2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 3cb93a386Sopenharmony_ci#include "tools/fiddle/examples.h" 4cb93a386Sopenharmony_ciREG_FIDDLE(subset_example, 512, 512, false, 3) { 5cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) { 6cb93a386Sopenharmony_ci canvas->drawColor(SK_ColorWHITE); 7cb93a386Sopenharmony_ci const int N = 8; 8cb93a386Sopenharmony_ci int shuffle[N * N]; 9cb93a386Sopenharmony_ci for (int i = 0; i < (N * N); ++i) { 10cb93a386Sopenharmony_ci shuffle[i] = i; 11cb93a386Sopenharmony_ci } 12cb93a386Sopenharmony_ci srand(0); 13cb93a386Sopenharmony_ci for (int i = 0; i < (N * N); ++i) { 14cb93a386Sopenharmony_ci std::swap(shuffle[i], shuffle[rand() % (N * N - i) + i]); 15cb93a386Sopenharmony_ci } 16cb93a386Sopenharmony_ci int w = (source.width() - 1) / N + 1; 17cb93a386Sopenharmony_ci int h = (source.height() - 1) / N + 1; 18cb93a386Sopenharmony_ci for (int i = 0; i < N; ++i) { 19cb93a386Sopenharmony_ci for (int j = 0; j < N; ++j) { 20cb93a386Sopenharmony_ci int x = shuffle[(N * i) + j] % N; 21cb93a386Sopenharmony_ci int y = shuffle[(N * i) + j] / N; 22cb93a386Sopenharmony_ci SkBitmap subset; 23cb93a386Sopenharmony_ci source.extractSubset(&subset, SkIRect::MakeXYWH(w * x, h * y, w, h)); 24cb93a386Sopenharmony_ci canvas->drawImage(subset.asImage(), w * i, h * j); 25cb93a386Sopenharmony_ci } 26cb93a386Sopenharmony_ci } 27cb93a386Sopenharmony_ci} 28cb93a386Sopenharmony_ci} // END FIDDLE 29