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