blob: 74299da4e646d0060b89f6bc0f2cb56ba6aca1a5 [file] [log] [blame]
scroggo@google.comd5764e82012-08-22 15:00:05 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
scroggo@google.com825bb952012-08-22 15:14:43 +00009#if SK_SUPPORT_GPU
10
scroggo@google.comd5764e82012-08-22 15:00:05 +000011#include "GrContext.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000012#include "GrContextFactory.h"
scroggo@google.comd5764e82012-08-22 15:00:05 +000013#include "SkBitmap.h"
scroggo@google.coma2a31922012-12-07 19:14:45 +000014#include "SkCanvas.h"
15#include "SkColor.h"
scroggo@google.comd5764e82012-08-22 15:00:05 +000016#include "SkGpuDevice.h"
scroggo@google.coma2a31922012-12-07 19:14:45 +000017#include "SkPaint.h"
scroggo@google.comd5764e82012-08-22 15:00:05 +000018#include "SkPixelRef.h"
19#include "SkRect.h"
20#include "Test.h"
21
22static const char* boolStr(bool value) {
23 return value ? "true" : "false";
24}
25
26// these are in the same order as the SkBitmap::Config enum
27static const char* gConfigName[] = {
28 "None", "4444", "8888"
29};
30
31struct Pair {
32 SkBitmap::Config fConfig;
33 const char* fValid;
34};
35
scroggo@google.coma2a31922012-12-07 19:14:45 +000036extern bool getUpperLeftFromOffset(const SkBitmap& bm, int* x, int* y);
37extern size_t getSubOffset(const SkBitmap& bm, int x, int y);
38
39/**
40 * Tests that getUpperLeftFromOffset and getSubOffset agree with each other.
41 */
42static void TestSubsetHelpers(skiatest::Reporter* reporter, const SkBitmap& bitmap){
43 int x, y;
44 bool upperLeft = getUpperLeftFromOffset(bitmap, &x, &y);
45 REPORTER_ASSERT(reporter, upperLeft);
46 REPORTER_ASSERT(reporter, getSubOffset(bitmap, x, y) == bitmap.pixelRefOffset());
47}
48
49/**
50 * Check to ensure that copying a GPU-backed SkBitmap behaved as expected.
51 * @param reporter Used to report failures.
52 * @param desiredConfig Config being copied to. If the copy succeeded, dst must have this Config.
53 * @param success True if the copy succeeded.
54 * @param src A GPU-backed SkBitmap that had copyTo or deepCopyTo called on it.
55 * @param dst SkBitmap that was copied to.
56 * @param deepCopy True if deepCopyTo was used; false if copyTo was used.
57 * @param subset Portion of src's SkPixelRef that src represents. dst should represent the same
58 * portion after the copy. Pass NULL for all pixels.
59 */
60static void TestIndividualCopy(skiatest::Reporter* reporter, const SkBitmap::Config desiredConfig,
61 const bool success, const SkBitmap& src, const SkBitmap& dst,
62 const bool deepCopy = true, const SkIRect* subset = NULL) {
63 if (success) {
64 REPORTER_ASSERT(reporter, src.width() == dst.width());
65 REPORTER_ASSERT(reporter, src.height() == dst.height());
66 REPORTER_ASSERT(reporter, dst.config() == desiredConfig);
67 if (src.config() == dst.config()) {
68 // FIXME: When calling copyTo (so deepCopy is false here), sometimes we copy the pixels
69 // exactly, in which case the IDs should be the same, but sometimes we do a bitmap draw,
70 // in which case the IDs should not be the same. Is there any way to determine which is
71 // the case at this point?
72 if (deepCopy) {
73 REPORTER_ASSERT(reporter, src.getGenerationID() == dst.getGenerationID());
74 }
75 REPORTER_ASSERT(reporter, src.pixelRef() != NULL && dst.pixelRef() != NULL);
76
77 // Do read backs and make sure that the two are the same.
78 SkBitmap srcReadBack, dstReadBack;
79 {
80 SkASSERT(src.getTexture() != NULL);
81 bool readBack = src.pixelRef()->readPixels(&srcReadBack, subset);
82 REPORTER_ASSERT(reporter, readBack);
83 }
84 if (dst.getTexture() != NULL) {
85 bool readBack = dst.pixelRef()->readPixels(&dstReadBack, subset);
86 REPORTER_ASSERT(reporter, readBack);
87 } else {
88 // If dst is not a texture, do a copy instead, to the same config as srcReadBack.
89 bool copy = dst.copyTo(&dstReadBack, srcReadBack.config());
90 REPORTER_ASSERT(reporter, copy);
91 }
92
93 SkAutoLockPixels srcLock(srcReadBack);
94 SkAutoLockPixels dstLock(dstReadBack);
95 REPORTER_ASSERT(reporter, srcReadBack.readyToDraw() && dstReadBack.readyToDraw());
96
97 const char* srcP = static_cast<const char*>(srcReadBack.getAddr(0, 0));
98 const char* dstP = static_cast<const char*>(dstReadBack.getAddr(0, 0));
99 REPORTER_ASSERT(reporter, srcP != dstP);
100
101 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, srcReadBack.getSize()));
102 } else {
103 REPORTER_ASSERT(reporter, src.getGenerationID() != dst.getGenerationID());
104 }
105
106 // If the copy used a subset, test the pixel offset calculation functions.
107 if (subset != NULL) {
108 TestSubsetHelpers(reporter, dst);
109 }
110 } else {
111 // dst should be unchanged from its initial state
112 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
113 REPORTER_ASSERT(reporter, dst.width() == 0);
114 REPORTER_ASSERT(reporter, dst.height() == 0);
115 }
116
117}
118
scroggo@google.comd5764e82012-08-22 15:00:05 +0000119// Stripped down version of TestBitmapCopy that checks basic fields (width, height, config, genID)
120// to ensure that they were copied properly.
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000121static void TestGpuBitmapCopy(skiatest::Reporter* reporter, GrContextFactory* factory) {
bsalomon@google.com7f805ff2012-12-10 17:32:07 +0000122#ifdef SK_BUILD_FOR_ANDROID // https://code.google.com/p/skia/issues/detail?id=753
123 return;
124#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000125 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
126 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
127 if (!GrContextFactory::IsRenderingGLContext(glType)) {
128 continue;
129 }
130#if SK_ANGLE // This test breaks ANGLE: memcmps fail and readpixels return value is false.
131 if (type == GrContextFactory::kANGLE_GLContextType) {
132 continue;
133 }
134#endif
scroggo@google.comd5764e82012-08-22 15:00:05 +0000135
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000136 GrContext* grContext = factory->get(glType);
137 if (NULL == grContext) {
138 continue;
scroggo@google.coma2a31922012-12-07 19:14:45 +0000139 }
140
scroggo@google.comd5764e82012-08-22 15:00:05 +0000141
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000142 if (NULL == grContext) {
143 return;
144 }
145 static const Pair gPairs[] = {
146 { SkBitmap::kNo_Config, "000" },
147 { SkBitmap::kARGB_4444_Config, "011" },
148 { SkBitmap::kARGB_8888_Config, "011" },
149 };
scroggo@google.comd5764e82012-08-22 15:00:05 +0000150
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000151 const int W = 20;
152 const int H = 33;
scroggo@google.coma2a31922012-12-07 19:14:45 +0000153
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000154 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
155 SkBitmap src, dst;
156
157 SkGpuDevice* device = SkNEW_ARGS(SkGpuDevice, (grContext, gPairs[i].fConfig, W, H));
158 SkAutoUnref aur(device);
159 src = device->accessBitmap(false);
160 device->clear(SK_ColorWHITE);
161
162 // Draw something different to the same portion of the bitmap that we will extract as a
163 // subset, so that comparing the pixels of the subset will be meaningful.
164 SkIRect subsetRect = SkIRect::MakeLTRB(W/2, H/2, W, H);
165 SkCanvas drawingCanvas(device);
166 SkPaint paint;
167 paint.setColor(SK_ColorRED);
168 drawingCanvas.drawRect(SkRect::MakeFromIRect(subsetRect), paint);
169
170 // Extract a subset. If this succeeds we will test copying the subset.
171 SkBitmap subset;
172 const bool extracted = src.extractSubset(&subset, subsetRect);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000173 if (extracted) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000174 TestSubsetHelpers(reporter, subset);
scroggo@google.comd5764e82012-08-22 15:00:05 +0000175 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000176
177 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
178 dst.reset();
179 bool success = src.deepCopyTo(&dst, gPairs[j].fConfig);
180 bool expected = gPairs[i].fValid[j] != '0';
181 if (success != expected) {
182 SkString str;
183 str.printf("SkBitmap::deepCopyTo from %s to %s. expected %s returned %s",
184 gConfigName[i], gConfigName[j], boolStr(expected),
185 boolStr(success));
186 reporter->reportFailed(str);
187 }
188
189 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
190 if (success != canSucceed) {
191 SkString str;
192 str.printf("SkBitmap::deepCopyTo from %s to %s returned %s,"
193 "but canCopyTo returned %s",
194 gConfigName[i], gConfigName[j], boolStr(success),
195 boolStr(canSucceed));
196 reporter->reportFailed(str);
197 }
198
199 TestIndividualCopy(reporter, gPairs[j].fConfig, success, src, dst);
200
201 // Test copying the subset bitmap, using both copyTo and deepCopyTo.
202 if (extracted) {
203 SkBitmap subsetCopy;
204 success = subset.copyTo(&subsetCopy, gPairs[j].fConfig);
205 REPORTER_ASSERT(reporter, success == expected);
206 REPORTER_ASSERT(reporter, success == canSucceed);
207 TestIndividualCopy(reporter, gPairs[j].fConfig, success, subset, subsetCopy, false,
208 &subsetRect);
209
210 // Reset the bitmap so that a failed copyTo will leave it in the expected state.
211 subsetCopy.reset();
212 success = subset.deepCopyTo(&subsetCopy, gPairs[j].fConfig);
213 REPORTER_ASSERT(reporter, success == expected);
214 REPORTER_ASSERT(reporter, success == canSucceed);
215 TestIndividualCopy(reporter, gPairs[j].fConfig, success, subset, subsetCopy, true,
216 &subsetRect);
217 }
218 } // for (size_t j = ...
219 } // for (size_t i = ...
220 } // GrContextFactory::GLContextType
scroggo@google.comd5764e82012-08-22 15:00:05 +0000221}
222
223#include "TestClassDef.h"
224DEFINE_GPUTESTCLASS("GpuBitmapCopy", TestGpuBitmapCopyClass, TestGpuBitmapCopy)
scroggo@google.com825bb952012-08-22 15:14:43 +0000225
226#endif