Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(871)

Side by Side Diff: tests/SpecialImageTest.cpp

Issue 1579323002: Add SkSpecialImage & SkSpecialSurface classes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Clean up Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 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 "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkImage.h"
11 #include "SkSpecialImage.h"
12 #include "SkSpecialSurface.h"
13 #include "Test.h"
14
15 #if SK_SUPPORT_GPU
16 #include "GrContext.h"
17 #endif
18
19 static const int kFullWidthHeight = 16;
20 static const int kSmallerWidthHeight = 10;
21
22 // Create a bitmap with red in the upper-left corner and green everywhere else
23 static SkBitmap create_bm() {
24 SkBitmap bm;
25 bm.allocN32Pixels(kFullWidthHeight, kFullWidthHeight, true);
26
27 SkCanvas temp(bm);
28
29 temp.clear(SK_ColorGREEN);
30 SkPaint p;
31 p.setColor(SK_ColorRED);
32 p.setAntiAlias(false);
33
34 temp.drawRect(SkRect::MakeIWH(kSmallerWidthHeight, kSmallerWidthHeight), p);
35
36 return bm;
37 }
38
39 // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
40 static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter,
41 bool peekPixelsSucceeds, bool peekTextureSucceeds) {
42 REPORTER_ASSERT(reporter, kSmallerWidthHeight == img->width());
43 REPORTER_ASSERT(reporter, kSmallerWidthHeight == img->height());
44
45 //--------------
46 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!img->peekTexture());
47
48 //--------------
49 SkImageInfo info;
50 size_t rowBytes;
51 REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!img->peekPixels(&info, &ro wBytes));
52 if (peekPixelsSucceeds) {
53 REPORTER_ASSERT(reporter, kFullWidthHeight == info.width());
54 REPORTER_ASSERT(reporter, kFullWidthHeight == info.height());
55 }
56
57 //--------------
58 info = SkImageInfo::MakeN32(kFullWidthHeight, kFullWidthHeight, kOpaque_SkAl phaType);
59
60 SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info));
61
62 SkCanvas* canvas = surf->getCanvas();
63
64 canvas->clear(SK_ColorBLUE);
65 img->draw(canvas, 0, 0, nullptr);
66
67 SkBitmap bm;
68 bm.allocN32Pixels(kFullWidthHeight, kFullWidthHeight, true);
69
70 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0 , 0);
71 SK_ALWAYSBREAK(result);
72
73 // Only the upper-left corner should've been drawn into the canvas
74 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerWidthHeight-1,
75 kSmallerWidthHeight-1)) ;
76 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerWidthHeight,
77 kSmallerWidthHeight)) ;
78 }
79
80 DEF_TEST(SpecialImage_Raster, reporter) {
81 SkBitmap bm = create_bm();
82
83 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::New(kSmallerWidthHeight, kS mallerWidthHeight,
84 bm));
85 test_image(img, reporter, true, false);
86 }
87
88 DEF_TEST(SpecialImage_Image, reporter) {
89 SkBitmap bm = create_bm();
90
91 SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm));
92
93 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::New(kSmallerWidthHeight, kS mallerWidthHeight,
94 fullImage));
95
96 test_image(img, reporter, true, false);
97 }
98
99 #if SK_SUPPORT_GPU
100 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
101 SkBitmap bm = create_bm();
102
103 GrSurfaceDesc desc;
104 desc.fConfig = kSkia8888_GrPixelConfig;
105 desc.fFlags = kNone_GrSurfaceFlags;
106 desc.fWidth = kFullWidthHeight;
107 desc.fHeight = kFullWidthHeight;
108
109 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(de sc, false,
110 bm .getPixels(), 0));
111 if (!texture) {
112 return;
113 }
114
115 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::New(kSmallerWidthHeight, kS mallerWidthHeight,
116 texture));
117 test_image(img, reporter, false, true);
118 }
119
120 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698