blob: fb81e2b74c70064fae2ee565bf885b30f79e1bee [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[] = {
reed@google.com6ba45722013-06-21 18:30:53 +000028 "None", "8888"
scroggo@google.comd5764e82012-08-22 15:00:05 +000029};
30
31struct Pair {
32 SkBitmap::Config fConfig;
33 const char* fValid;
34};
35
scroggo@google.coma2a31922012-12-07 19:14:45 +000036/**
37 * Check to ensure that copying a GPU-backed SkBitmap behaved as expected.
38 * @param reporter Used to report failures.
39 * @param desiredConfig Config being copied to. If the copy succeeded, dst must have this Config.
40 * @param success True if the copy succeeded.
41 * @param src A GPU-backed SkBitmap that had copyTo or deepCopyTo called on it.
42 * @param dst SkBitmap that was copied to.
scroggo@google.com5ccae2c2014-01-15 16:56:52 +000043 * @param expectSameGenID Whether the genIDs should be the same if success is true.
scroggo@google.coma2a31922012-12-07 19:14:45 +000044 */
45static void TestIndividualCopy(skiatest::Reporter* reporter, const SkBitmap::Config desiredConfig,
46 const bool success, const SkBitmap& src, const SkBitmap& dst,
scroggo@google.com5ccae2c2014-01-15 16:56:52 +000047 const bool expectSameGenID) {
scroggo@google.coma2a31922012-12-07 19:14:45 +000048 if (success) {
49 REPORTER_ASSERT(reporter, src.width() == dst.width());
50 REPORTER_ASSERT(reporter, src.height() == dst.height());
51 REPORTER_ASSERT(reporter, dst.config() == desiredConfig);
52 if (src.config() == dst.config()) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +000053 if (expectSameGenID) {
scroggo@google.coma2a31922012-12-07 19:14:45 +000054 REPORTER_ASSERT(reporter, src.getGenerationID() == dst.getGenerationID());
scroggo@google.com5ccae2c2014-01-15 16:56:52 +000055 } else {
56 REPORTER_ASSERT(reporter, src.getGenerationID() != dst.getGenerationID());
scroggo@google.coma2a31922012-12-07 19:14:45 +000057 }
58 REPORTER_ASSERT(reporter, src.pixelRef() != NULL && dst.pixelRef() != NULL);
59
60 // Do read backs and make sure that the two are the same.
61 SkBitmap srcReadBack, dstReadBack;
62 {
63 SkASSERT(src.getTexture() != NULL);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +000064 const SkIPoint origin = src.pixelRefOrigin();
65 const SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY,
66 src.width(), src.height());
67 bool readBack = src.pixelRef()->readPixels(&srcReadBack, &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +000068 REPORTER_ASSERT(reporter, readBack);
69 }
70 if (dst.getTexture() != NULL) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +000071 const SkIPoint origin = dst.pixelRefOrigin();
72 const SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY,
73 dst.width(), dst.height());
74 bool readBack = dst.pixelRef()->readPixels(&dstReadBack, &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +000075 REPORTER_ASSERT(reporter, readBack);
76 } else {
77 // If dst is not a texture, do a copy instead, to the same config as srcReadBack.
78 bool copy = dst.copyTo(&dstReadBack, srcReadBack.config());
79 REPORTER_ASSERT(reporter, copy);
80 }
81
82 SkAutoLockPixels srcLock(srcReadBack);
83 SkAutoLockPixels dstLock(dstReadBack);
84 REPORTER_ASSERT(reporter, srcReadBack.readyToDraw() && dstReadBack.readyToDraw());
85
86 const char* srcP = static_cast<const char*>(srcReadBack.getAddr(0, 0));
87 const char* dstP = static_cast<const char*>(dstReadBack.getAddr(0, 0));
88 REPORTER_ASSERT(reporter, srcP != dstP);
89
90 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, srcReadBack.getSize()));
91 } else {
92 REPORTER_ASSERT(reporter, src.getGenerationID() != dst.getGenerationID());
93 }
scroggo@google.coma2a31922012-12-07 19:14:45 +000094 } else {
95 // dst should be unchanged from its initial state
96 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
97 REPORTER_ASSERT(reporter, dst.width() == 0);
98 REPORTER_ASSERT(reporter, dst.height() == 0);
99 }
100
101}
102
scroggo@google.comd5764e82012-08-22 15:00:05 +0000103// Stripped down version of TestBitmapCopy that checks basic fields (width, height, config, genID)
104// to ensure that they were copied properly.
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000105DEF_GPUTEST(GpuBitmapCopy, reporter, factory) {
bsalomon@google.com7f805ff2012-12-10 17:32:07 +0000106#ifdef SK_BUILD_FOR_ANDROID // https://code.google.com/p/skia/issues/detail?id=753
107 return;
108#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000109 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
110 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
111 if (!GrContextFactory::IsRenderingGLContext(glType)) {
112 continue;
113 }
scroggo@google.comd5764e82012-08-22 15:00:05 +0000114
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000115 GrContext* grContext = factory->get(glType);
116 if (NULL == grContext) {
117 continue;
scroggo@google.coma2a31922012-12-07 19:14:45 +0000118 }
119
scroggo@google.comd5764e82012-08-22 15:00:05 +0000120
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000121 if (NULL == grContext) {
122 return;
123 }
124 static const Pair gPairs[] = {
reed@google.comea338082013-06-21 19:05:57 +0000125 { SkBitmap::kNo_Config, "00" },
126 { SkBitmap::kARGB_8888_Config, "01" },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000127 };
scroggo@google.comd5764e82012-08-22 15:00:05 +0000128
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000129 const int W = 20;
130 const int H = 33;
scroggo@google.coma2a31922012-12-07 19:14:45 +0000131
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000132 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
133 SkBitmap src, dst;
134
135 SkGpuDevice* device = SkNEW_ARGS(SkGpuDevice, (grContext, gPairs[i].fConfig, W, H));
136 SkAutoUnref aur(device);
137 src = device->accessBitmap(false);
138 device->clear(SK_ColorWHITE);
139
140 // Draw something different to the same portion of the bitmap that we will extract as a
141 // subset, so that comparing the pixels of the subset will be meaningful.
142 SkIRect subsetRect = SkIRect::MakeLTRB(W/2, H/2, W, H);
143 SkCanvas drawingCanvas(device);
144 SkPaint paint;
145 paint.setColor(SK_ColorRED);
reed@google.com44699382013-10-31 17:28:30 +0000146 drawingCanvas.drawRect(SkRect::Make(subsetRect), paint);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000147
148 // Extract a subset. If this succeeds we will test copying the subset.
149 SkBitmap subset;
150 const bool extracted = src.extractSubset(&subset, subsetRect);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000151
152 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
153 dst.reset();
154 bool success = src.deepCopyTo(&dst, gPairs[j].fConfig);
155 bool expected = gPairs[i].fValid[j] != '0';
156 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000157 ERRORF(reporter, "SkBitmap::deepCopyTo from %s to %s. "
158 "expected %s returned %s", gConfigName[i],
159 gConfigName[j], boolStr(expected),
160 boolStr(success));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000161 }
162
163 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
164 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000165 ERRORF(reporter, "SkBitmap::deepCopyTo from %s to %s "
166 "returned %s, but canCopyTo returned %s",
167 gConfigName[i], gConfigName[j], boolStr(success),
168 boolStr(canSucceed));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000169 }
170
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000171 TestIndividualCopy(reporter, gPairs[j].fConfig, success, src, dst, true);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000172
173 // Test copying the subset bitmap, using both copyTo and deepCopyTo.
174 if (extracted) {
175 SkBitmap subsetCopy;
176 success = subset.copyTo(&subsetCopy, gPairs[j].fConfig);
177 REPORTER_ASSERT(reporter, success == expected);
178 REPORTER_ASSERT(reporter, success == canSucceed);
scroggo@google.com93491012013-02-05 19:22:27 +0000179 TestIndividualCopy(reporter, gPairs[j].fConfig, success, subset, subsetCopy,
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000180 true);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000181
182 // Reset the bitmap so that a failed copyTo will leave it in the expected state.
183 subsetCopy.reset();
184 success = subset.deepCopyTo(&subsetCopy, gPairs[j].fConfig);
185 REPORTER_ASSERT(reporter, success == expected);
186 REPORTER_ASSERT(reporter, success == canSucceed);
scroggo@google.com93491012013-02-05 19:22:27 +0000187 TestIndividualCopy(reporter, gPairs[j].fConfig, success, subset, subsetCopy,
188 true);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000189
190 // Now set a bitmap to be a subset that will share the same pixelref.
191 // This allows testing another case of cloning the genID. When calling copyTo
192 // on a bitmap representing a subset of its pixelref, the resulting pixelref
193 // should not share the genID, since we only copied the subset.
194 SkBitmap trueSubset;
195 // FIXME: Once https://codereview.chromium.org/109023008/ lands, call
196 // trueSubset.installPixelRef(src.pixelRef(), subset);
197 trueSubset.setConfig(gPairs[i].fConfig, W/2, H/2);
198 trueSubset.setPixelRef(src.pixelRef(), W/2, H/2);
199
200 subsetCopy.reset();
201 success = trueSubset.copyTo(&subsetCopy, gPairs[j].fConfig);
202 REPORTER_ASSERT(reporter, success == expected);
203 REPORTER_ASSERT(reporter, success == canSucceed);
204 TestIndividualCopy(reporter, gPairs[j].fConfig, success, trueSubset, subsetCopy,
205 false);
206
207 // deepCopyTo copies the entire pixelref, even if the bitmap only represents
208 // a subset. Therefore, the result should share the same genID.
209 subsetCopy.reset();
210 success = trueSubset.deepCopyTo(&subsetCopy, gPairs[j].fConfig);
211 REPORTER_ASSERT(reporter, success == expected);
212 REPORTER_ASSERT(reporter, success == canSucceed);
213 TestIndividualCopy(reporter, gPairs[j].fConfig, success, trueSubset, subsetCopy,
214 true);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000215 }
216 } // for (size_t j = ...
217 } // for (size_t i = ...
218 } // GrContextFactory::GLContextType
scroggo@google.comd5764e82012-08-22 15:00:05 +0000219}
220
scroggo@google.com825bb952012-08-22 15:14:43 +0000221#endif