blob: 9bde7eec3348546474bbbc377cbfbc87139a099f [file] [log] [blame]
robertphillipsb6c65e92016-02-04 10:52:42 -08001/*
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"
robertphillips4418dba2016-03-07 12:45:14 -080014#include "TestingSpecialImageAccess.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080015
16#if SK_SUPPORT_GPU
17#include "GrContext.h"
18#endif
19
robertphillipsb6c65e92016-02-04 10:52:42 -080020
21// This test creates backing resources exactly sized to [kFullSize x kFullSize].
22// It then wraps them in an SkSpecialImage with only the center (red) region being active.
23// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
24// of the inactive (green) region leaked out.
25
26static const int kSmallerSize = 10;
27static const int kPad = 3;
28static const int kFullSize = kSmallerSize + 2 * kPad;
29
30// Create a bitmap with red in the center and green around it
31static SkBitmap create_bm() {
32 SkBitmap bm;
33 bm.allocN32Pixels(kFullSize, kFullSize, true);
34
35 SkCanvas temp(bm);
36
37 temp.clear(SK_ColorGREEN);
38 SkPaint p;
39 p.setColor(SK_ColorRED);
40 p.setAntiAlias(false);
41
42 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
43 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
44 p);
45
46 return bm;
47}
48
49// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
50static void test_image(SkSpecialImage* img, skiatest::Reporter* reporter,
robertphillips19dea942016-03-16 10:39:08 -070051 bool peekPixelsSucceeds, bool peekTextureSucceeds) {
robertphillipsb6c65e92016-02-04 10:52:42 -080052 const SkIRect subset = TestingSpecialImageAccess::Subset(img);
robertphillips19dea942016-03-16 10:39:08 -070053 REPORTER_ASSERT(reporter, kPad == subset.left());
54 REPORTER_ASSERT(reporter, kPad == subset.top());
robertphillipsb6c65e92016-02-04 10:52:42 -080055 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
56 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
57
58 //--------------
59 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!TestingSpecialImageAccess::PeekTexture(img));
60
61 //--------------
62 SkPixmap pixmap;
63 REPORTER_ASSERT(reporter, peekPixelsSucceeds ==
64 !!TestingSpecialImageAccess::PeekPixels(img, &pixmap));
65 if (peekPixelsSucceeds) {
robertphillips19dea942016-03-16 10:39:08 -070066 REPORTER_ASSERT(reporter, kFullSize == pixmap.width());
67 REPORTER_ASSERT(reporter, kFullSize == pixmap.height());
robertphillipsb6c65e92016-02-04 10:52:42 -080068 }
69
70 //--------------
71 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
72
73 SkAutoTUnref<SkSpecialSurface> surf(img->newSurface(info));
74
75 SkCanvas* canvas = surf->getCanvas();
76
77 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080078 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080079
80 SkBitmap bm;
81 bm.allocN32Pixels(kFullSize, kFullSize, true);
82
83 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
84 SkASSERT_RELEASE(result);
85
86 // Only the center (red) portion should've been drawn into the canvas
87 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
88 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
89 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
90 kSmallerSize+kPad-1));
91 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
92 kSmallerSize+kPad));
93}
94
95DEF_TEST(SpecialImage_Raster, reporter) {
96 SkBitmap bm = create_bm();
97
98 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
99
robertphillips19dea942016-03-16 10:39:08 -0700100 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromRaster(nullptr, subset, bm));
101 test_image(img, reporter, true, false);
robertphillipsb6c65e92016-02-04 10:52:42 -0800102}
103
104DEF_TEST(SpecialImage_Image, reporter) {
105 SkBitmap bm = create_bm();
106
107 SkAutoTUnref<SkImage> fullImage(SkImage::NewFromBitmap(bm));
108
109 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
110
robertphillips19dea942016-03-16 10:39:08 -0700111 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromImage(nullptr, subset, fullImage));
112 test_image(img, reporter, true, false);
robertphillipsb6c65e92016-02-04 10:52:42 -0800113}
114
115#if SK_SUPPORT_GPU
116DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
117 SkBitmap bm = create_bm();
118
119 GrSurfaceDesc desc;
120 desc.fConfig = kSkia8888_GrPixelConfig;
121 desc.fFlags = kNone_GrSurfaceFlags;
122 desc.fWidth = kFullSize;
123 desc.fHeight = kFullSize;
124
bsalomon5ec26ae2016-02-25 08:33:02 -0800125 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, SkBudgeted::kNo,
robertphillipsb6c65e92016-02-04 10:52:42 -0800126 bm.getPixels(), 0));
127 if (!texture) {
128 return;
129 }
130
131 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
132
robertphillips19dea942016-03-16 10:39:08 -0700133 SkAutoTUnref<SkSpecialImage> img(SkSpecialImage::NewFromGpu(nullptr, subset,
134 kNeedNewImageUniqueID_SpecialImage,
135 texture));
136 test_image(img, reporter, false, true);
robertphillipsb6c65e92016-02-04 10:52:42 -0800137}
138
139#endif