blob: 9d1a527be0e8220c4b5fdcff71bd2825823645ad [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"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000021#include "TestClassDef.h"
scroggo@google.comd5764e82012-08-22 15:00:05 +000022
23static const char* boolStr(bool value) {
24 return value ? "true" : "false";
25}
26
27// these are in the same order as the SkBitmap::Config enum
28static const char* gConfigName[] = {
reed@google.com6ba45722013-06-21 18:30:53 +000029 "None", "8888"
scroggo@google.comd5764e82012-08-22 15:00:05 +000030};
31
32struct Pair {
33 SkBitmap::Config fConfig;
34 const char* fValid;
35};
36
scroggo@google.coma2a31922012-12-07 19:14:45 +000037/**
38 * Check to ensure that copying a GPU-backed SkBitmap behaved as expected.
39 * @param reporter Used to report failures.
40 * @param desiredConfig Config being copied to. If the copy succeeded, dst must have this Config.
41 * @param success True if the copy succeeded.
42 * @param src A GPU-backed SkBitmap that had copyTo or deepCopyTo called on it.
43 * @param dst SkBitmap that was copied to.
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +000044 * @param deepCopy True if deepCopyTo was used; false if copyTo was used.
scroggo@google.coma2a31922012-12-07 19:14:45 +000045 */
46static void TestIndividualCopy(skiatest::Reporter* reporter, const SkBitmap::Config desiredConfig,
47 const bool success, const SkBitmap& src, const SkBitmap& dst,
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +000048 const bool deepCopy = true) {
scroggo@google.coma2a31922012-12-07 19:14:45 +000049 if (success) {
50 REPORTER_ASSERT(reporter, src.width() == dst.width());
51 REPORTER_ASSERT(reporter, src.height() == dst.height());
52 REPORTER_ASSERT(reporter, dst.config() == desiredConfig);
53 if (src.config() == dst.config()) {
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +000054 // FIXME: When calling copyTo (so deepCopy is false here), sometimes we copy the pixels
55 // exactly, in which case the IDs should be the same, but sometimes we do a bitmap draw,
56 // in which case the IDs should not be the same. Is there any way to determine which is
57 // the case at this point?
58 if (deepCopy) {
scroggo@google.coma2a31922012-12-07 19:14:45 +000059 REPORTER_ASSERT(reporter, src.getGenerationID() == dst.getGenerationID());
60 }
61 REPORTER_ASSERT(reporter, src.pixelRef() != NULL && dst.pixelRef() != NULL);
62
63 // Do read backs and make sure that the two are the same.
64 SkBitmap srcReadBack, dstReadBack;
65 {
66 SkASSERT(src.getTexture() != NULL);
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +000067 bool readBack = src.pixelRef()->readPixels(&srcReadBack);
scroggo@google.coma2a31922012-12-07 19:14:45 +000068 REPORTER_ASSERT(reporter, readBack);
69 }
70 if (dst.getTexture() != NULL) {
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +000071 bool readBack = dst.pixelRef()->readPixels(&dstReadBack);
scroggo@google.coma2a31922012-12-07 19:14:45 +000072 REPORTER_ASSERT(reporter, readBack);
73 } else {
74 // If dst is not a texture, do a copy instead, to the same config as srcReadBack.
75 bool copy = dst.copyTo(&dstReadBack, srcReadBack.config());
76 REPORTER_ASSERT(reporter, copy);
77 }
78
79 SkAutoLockPixels srcLock(srcReadBack);
80 SkAutoLockPixels dstLock(dstReadBack);
81 REPORTER_ASSERT(reporter, srcReadBack.readyToDraw() && dstReadBack.readyToDraw());
82
83 const char* srcP = static_cast<const char*>(srcReadBack.getAddr(0, 0));
84 const char* dstP = static_cast<const char*>(dstReadBack.getAddr(0, 0));
85 REPORTER_ASSERT(reporter, srcP != dstP);
86
87 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, srcReadBack.getSize()));
88 } else {
89 REPORTER_ASSERT(reporter, src.getGenerationID() != dst.getGenerationID());
90 }
scroggo@google.coma2a31922012-12-07 19:14:45 +000091 } else {
92 // dst should be unchanged from its initial state
93 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
94 REPORTER_ASSERT(reporter, dst.width() == 0);
95 REPORTER_ASSERT(reporter, dst.height() == 0);
96 }
97
98}
99
scroggo@google.comd5764e82012-08-22 15:00:05 +0000100// Stripped down version of TestBitmapCopy that checks basic fields (width, height, config, genID)
101// to ensure that they were copied properly.
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000102DEF_GPUTEST(GpuBitmapCopy, reporter, factory) {
bsalomon@google.com7f805ff2012-12-10 17:32:07 +0000103#ifdef SK_BUILD_FOR_ANDROID // https://code.google.com/p/skia/issues/detail?id=753
104 return;
105#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000106 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
107 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
108 if (!GrContextFactory::IsRenderingGLContext(glType)) {
109 continue;
110 }
scroggo@google.comd5764e82012-08-22 15:00:05 +0000111
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000112 GrContext* grContext = factory->get(glType);
113 if (NULL == grContext) {
114 continue;
scroggo@google.coma2a31922012-12-07 19:14:45 +0000115 }
116
scroggo@google.comd5764e82012-08-22 15:00:05 +0000117
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000118 if (NULL == grContext) {
119 return;
120 }
121 static const Pair gPairs[] = {
reed@google.comea338082013-06-21 19:05:57 +0000122 { SkBitmap::kNo_Config, "00" },
123 { SkBitmap::kARGB_8888_Config, "01" },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000124 };
scroggo@google.comd5764e82012-08-22 15:00:05 +0000125
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000126 const int W = 20;
127 const int H = 33;
scroggo@google.coma2a31922012-12-07 19:14:45 +0000128
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000129 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
130 SkBitmap src, dst;
131
132 SkGpuDevice* device = SkNEW_ARGS(SkGpuDevice, (grContext, gPairs[i].fConfig, W, H));
133 SkAutoUnref aur(device);
134 src = device->accessBitmap(false);
135 device->clear(SK_ColorWHITE);
136
137 // Draw something different to the same portion of the bitmap that we will extract as a
138 // subset, so that comparing the pixels of the subset will be meaningful.
139 SkIRect subsetRect = SkIRect::MakeLTRB(W/2, H/2, W, H);
140 SkCanvas drawingCanvas(device);
141 SkPaint paint;
142 paint.setColor(SK_ColorRED);
reed@google.com44699382013-10-31 17:28:30 +0000143 drawingCanvas.drawRect(SkRect::Make(subsetRect), paint);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000144
145 // Extract a subset. If this succeeds we will test copying the subset.
146 SkBitmap subset;
147 const bool extracted = src.extractSubset(&subset, subsetRect);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000148
149 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
150 dst.reset();
151 bool success = src.deepCopyTo(&dst, gPairs[j].fConfig);
152 bool expected = gPairs[i].fValid[j] != '0';
153 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000154 ERRORF(reporter, "SkBitmap::deepCopyTo from %s to %s. "
155 "expected %s returned %s", gConfigName[i],
156 gConfigName[j], boolStr(expected),
157 boolStr(success));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000158 }
159
160 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
161 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000162 ERRORF(reporter, "SkBitmap::deepCopyTo from %s to %s "
163 "returned %s, but canCopyTo returned %s",
164 gConfigName[i], gConfigName[j], boolStr(success),
165 boolStr(canSucceed));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000166 }
167
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000168 TestIndividualCopy(reporter, gPairs[j].fConfig, success, src, dst);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000169
170 // Test copying the subset bitmap, using both copyTo and deepCopyTo.
171 if (extracted) {
172 SkBitmap subsetCopy;
173 success = subset.copyTo(&subsetCopy, gPairs[j].fConfig);
174 REPORTER_ASSERT(reporter, success == expected);
175 REPORTER_ASSERT(reporter, success == canSucceed);
scroggo@google.com93491012013-02-05 19:22:27 +0000176 TestIndividualCopy(reporter, gPairs[j].fConfig, success, subset, subsetCopy,
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000177 false);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000178
179 // Reset the bitmap so that a failed copyTo will leave it in the expected state.
180 subsetCopy.reset();
181 success = subset.deepCopyTo(&subsetCopy, gPairs[j].fConfig);
182 REPORTER_ASSERT(reporter, success == expected);
183 REPORTER_ASSERT(reporter, success == canSucceed);
scroggo@google.com93491012013-02-05 19:22:27 +0000184 TestIndividualCopy(reporter, gPairs[j].fConfig, success, subset, subsetCopy,
185 true);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000186 }
187 } // for (size_t j = ...
188 } // for (size_t i = ...
189 } // GrContextFactory::GLContextType
scroggo@google.comd5764e82012-08-22 15:00:05 +0000190}
191
scroggo@google.com825bb952012-08-22 15:14:43 +0000192#endif