Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b021f3e

Browse files
HalCanarySkia Commit-Bot
authored andcommitted
Sample/Filter2View: make it work without /skimages
Change-Id: I8958e026ca9e2c5a6c99a7a1cd1ec97088c25fe9 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/225548 Commit-Queue: Hal Canary <[email protected]> Reviewed-by: Ben Wagner <[email protected]>
1 parent b3b98b7 commit b021f3e

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

samplecode/DecodeFile.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
#include "include/core/SkData.h"
1414
#include "include/core/SkImage.h"
1515

16-
static inline bool decode_file(const char* filename, SkBitmap* bitmap,
16+
static inline bool decode_file(sk_sp<SkData> data, SkBitmap* bitmap,
1717
SkColorType colorType = kN32_SkColorType,
1818
bool requireUnpremul = false) {
19-
sk_sp<SkData> data(SkData::MakeFromFileName(filename));
20-
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
19+
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(data));
2120
if (!codec) {
2221
return false;
2322
}
@@ -34,6 +33,12 @@ static inline bool decode_file(const char* filename, SkBitmap* bitmap,
3433
return SkCodec::kSuccess == codec->getPixels(info, bitmap->getPixels(), bitmap->rowBytes());
3534
}
3635

36+
static inline bool decode_file(const char* filename, SkBitmap* bitmap,
37+
SkColorType colorType = kN32_SkColorType,
38+
bool requireUnpremul = false) {
39+
return decode_file(SkData::MakeFromFileName(filename), bitmap, colorType, requireUnpremul);
40+
}
41+
3742
static inline sk_sp<SkImage> decode_file(const char filename[]) {
3843
sk_sp<SkData> data(SkData::MakeFromFileName(filename));
3944
return data ? SkImage::MakeFromEncoded(data) : nullptr;

samplecode/SampleFilter2.cpp

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,40 @@
66
*/
77

88
#include "include/core/SkCanvas.h"
9-
#include "include/core/SkColorFilter.h"
10-
#include "include/core/SkColorPriv.h"
11-
#include "include/core/SkGraphics.h"
12-
#include "include/core/SkPath.h"
13-
#include "include/core/SkRegion.h"
14-
#include "include/core/SkShader.h"
9+
#include "include/core/SkFont.h"
1510
#include "include/core/SkString.h"
16-
#include "include/core/SkTime.h"
17-
#include "include/effects/SkGradientShader.h"
1811
#include "include/utils/SkTextUtils.h"
1912
#include "samplecode/DecodeFile.h"
2013
#include "samplecode/Sample.h"
21-
#include "src/utils/SkUTF.h"
14+
#include "tools/Resources.h"
15+
16+
#include <vector>
2217

2318
static const char* gNames[] = {
24-
"/skimages/background_01.png"
19+
"images/mandrill_512_q075.jpg",
20+
"images/dog.jpg",
2521
};
2622

27-
class Filter2View : public Sample {
28-
public:
29-
std::unique_ptr<SkBitmap[]> fBitmaps;
30-
int fBitmapCount;
31-
int fCurrIndex;
32-
33-
Filter2View() {
34-
fBitmapCount = SK_ARRAY_COUNT(gNames)*2;
35-
fBitmaps.reset(new SkBitmap[fBitmapCount]);
23+
struct Filter2View : public Sample {
24+
std::vector<SkBitmap> fBitmaps;
3625

37-
for (int i = 0; i < fBitmapCount/2; i++) {
38-
decode_file(gNames[i], &fBitmaps[i]);
26+
void onOnceBeforeDraw() override {
27+
SkASSERT(fBitmaps.empty());
28+
fBitmaps.reserve(SK_ARRAY_COUNT(gNames) * 2);
29+
for (const char* name : gNames) {
30+
SkBitmap bitmap;
31+
(void)decode_file(GetResourceAsData(name), &bitmap);
32+
fBitmaps.push_back(std::move(bitmap));
3933
}
40-
for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
41-
decode_file(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType);
34+
for (const char* name : gNames) {
35+
SkBitmap bitmap;
36+
(void)decode_file(GetResourceAsData(name), &bitmap, kRGB_565_SkColorType);
37+
fBitmaps.push_back(std::move(bitmap));
4238
}
43-
fCurrIndex = 0;
44-
4539
this->setBGColor(SK_ColorGRAY);
4640
}
4741

48-
protected:
49-
SkString name() override { return SkStringPrintf("Filter/Dither %s", gNames[fCurrIndex]); }
42+
SkString name() override { return SkString("Filter/Dither"); }
5043

5144
void onDrawContent(SkCanvas* canvas) override {
5245
canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
@@ -62,8 +55,8 @@ class Filter2View : public Sample {
6255
paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
6356
for (int j = 0; j < 2; j++) {
6457
paint.setDither(j == 1);
65-
for (int i = 0; i < fBitmapCount; i++) {
66-
SkScalar x = (k * fBitmapCount + j) * W;
58+
for (int i = 0; i < (int)fBitmaps.size(); i++) {
59+
SkScalar x = (k * (int)fBitmaps.size() + j) * W;
6760
SkScalar y = i * H;
6861
x = SkScalarRoundToScalar(x);
6962
y = SkScalarRoundToScalar(y);
@@ -88,11 +81,5 @@ class Filter2View : public Sample {
8881
}
8982
}
9083
}
91-
92-
private:
93-
typedef Sample INHERITED;
9484
};
95-
96-
//////////////////////////////////////////////////////////////////////////////
97-
9885
DEF_SAMPLE( return new Filter2View(); )

0 commit comments

Comments
 (0)