blob: df05f406c413a3ce49ae7bd91765f3ceb47b9347 [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
9#include "GrContext.h"
10#include "SkBitmap.h"
11#include "SkGpuDevice.h"
12#include "SkPixelRef.h"
13#include "SkRect.h"
14#include "Test.h"
15
16static const char* boolStr(bool value) {
17 return value ? "true" : "false";
18}
19
20// these are in the same order as the SkBitmap::Config enum
21static const char* gConfigName[] = {
22 "None", "4444", "8888"
23};
24
25struct Pair {
26 SkBitmap::Config fConfig;
27 const char* fValid;
28};
29
30// Stripped down version of TestBitmapCopy that checks basic fields (width, height, config, genID)
31// to ensure that they were copied properly.
32static void TestGpuBitmapCopy(skiatest::Reporter* reporter, GrContext* grContext) {
33 if (NULL == grContext) {
34 return;
35 }
36 static const Pair gPairs[] = {
37 { SkBitmap::kNo_Config, "000" },
38 { SkBitmap::kARGB_4444_Config, "011" },
39 { SkBitmap::kARGB_8888_Config, "011" },
40 };
41
42 const int W = 20;
43 const int H = 33;
44
45 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
46 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
47 SkBitmap src, dst;
48
49 SkGpuDevice* device = SkNEW_ARGS(SkGpuDevice, (grContext, gPairs[i].fConfig, W, H));
50 SkAutoUnref aur(device);
51 src = device->accessBitmap(false);
52 device->clear(SK_ColorWHITE);
53
54 bool success = src.deepCopyTo(&dst, gPairs[j].fConfig);
55 bool expected = gPairs[i].fValid[j] != '0';
56 if (success != expected) {
57 SkString str;
58 str.printf("SkBitmap::deepCopyTo from %s to %s. expected %s returned %s",
59 gConfigName[i], gConfigName[j], boolStr(expected),
60 boolStr(success));
61 reporter->reportFailed(str);
62 }
63
64 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
65 if (success != canSucceed) {
66 SkString str;
67 str.printf("SkBitmap::deepCopyTo from %s to %s returned %s,"
68 "but canCopyTo returned %s",
69 gConfigName[i], gConfigName[j], boolStr(success),
70 boolStr(canSucceed));
71 reporter->reportFailed(str);
72 }
73
74 if (success) {
75 REPORTER_ASSERT(reporter, src.width() == dst.width());
76 REPORTER_ASSERT(reporter, src.height() == dst.height());
77 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
78 if (src.config() == dst.config()) {
79 REPORTER_ASSERT(reporter, src.getGenerationID() == dst.getGenerationID());
80 // Do read backs and make sure that the two are the same.
81 SkBitmap srcReadBack, dstReadBack;
82 REPORTER_ASSERT(reporter, src.pixelRef() != NULL
83 && dst.pixelRef() != NULL);
84 src.pixelRef()->readPixels(&srcReadBack);
85 dst.pixelRef()->readPixels(&dstReadBack);
86 SkAutoLockPixels srcLock(srcReadBack);
87 SkAutoLockPixels dstLock(dstReadBack);
88 REPORTER_ASSERT(reporter, srcReadBack.readyToDraw()
89 && dstReadBack.readyToDraw());
90 const char* srcP = (const char*)srcReadBack.getAddr(0, 0);
91 const char* dstP = (const char*)dstReadBack.getAddr(0, 0);
92 REPORTER_ASSERT(reporter, srcP != dstP);
93 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, srcReadBack.getSize()));
94 } else {
95 REPORTER_ASSERT(reporter, src.getGenerationID() != dst.getGenerationID());
96 }
97 } else {
98 // dst should be unchanged from its initial state
99 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
100 REPORTER_ASSERT(reporter, dst.width() == 0);
101 REPORTER_ASSERT(reporter, dst.height() == 0);
102 }
103 } // for (size_t j = ...
104 }
105}
106
107#include "TestClassDef.h"
108DEFINE_GPUTESTCLASS("GpuBitmapCopy", TestGpuBitmapCopyClass, TestGpuBitmapCopy)