ReadPixelsTest.cpp revision 6950de6c4166fabb35e6c756fc009e0cf1c47819
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCanvas.h"
9#include "SkColorPriv.h"
10#include "SkMathPriv.h"
11#include "SkRegion.h"
12#include "SkSurface.h"
13#include "Test.h"
14
15#if SK_SUPPORT_GPU
16#include "GrContextFactory.h"
17#include "SkGpuDevice.h"
18#include "SkGr.h"
19#endif
20
21static const int DEV_W = 100, DEV_H = 100;
22static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
23static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
24                                                DEV_H * SK_Scalar1);
25
26static SkPMColor get_src_color(int x, int y) {
27    SkASSERT(x >= 0 && x < DEV_W);
28    SkASSERT(y >= 0 && y < DEV_H);
29
30    U8CPU r = x;
31    U8CPU g = y;
32    U8CPU b = 0xc;
33
34    U8CPU a = 0xff;
35    switch ((x+y) % 5) {
36        case 0:
37            a = 0xff;
38            break;
39        case 1:
40            a = 0x80;
41            break;
42        case 2:
43            a = 0xCC;
44            break;
45        case 4:
46            a = 0x01;
47            break;
48        case 3:
49            a = 0x00;
50            break;
51    }
52    return SkPremultiplyARGBInline(a, r, g, b);
53}
54
55static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
56    int n = y * w + x;
57
58    U8CPU b = n & 0xff;
59    U8CPU g = (n >> 8) & 0xff;
60    U8CPU r = (n >> 16) & 0xff;
61    return SkPackARGB32(0xff, r, g , b);
62}
63
64static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
65                                    bool* doUnpremul) {
66    *doUnpremul = (kUnpremul_SkAlphaType == at);
67
68    const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
69    U8CPU a,r,g,b;
70    switch (ct) {
71        case kBGRA_8888_SkColorType:
72            b = static_cast<U8CPU>(c[0]);
73            g = static_cast<U8CPU>(c[1]);
74            r = static_cast<U8CPU>(c[2]);
75            a = static_cast<U8CPU>(c[3]);
76            break;
77        case kRGBA_8888_SkColorType:
78            r = static_cast<U8CPU>(c[0]);
79            g = static_cast<U8CPU>(c[1]);
80            b = static_cast<U8CPU>(c[2]);
81            a = static_cast<U8CPU>(c[3]);
82            break;
83        default:
84            SkDEBUGFAIL("Unexpected colortype");
85            return 0;
86    }
87
88    if (*doUnpremul) {
89        r = SkMulDiv255Ceiling(r, a);
90        g = SkMulDiv255Ceiling(g, a);
91        b = SkMulDiv255Ceiling(b, a);
92    }
93    return SkPackARGB32(a, r, g, b);
94}
95
96static SkBitmap make_src_bitmap() {
97    static SkBitmap bmp;
98    if (bmp.isNull()) {
99        bmp.allocN32Pixels(DEV_W, DEV_H);
100        intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
101        for (int y = 0; y < DEV_H; ++y) {
102            for (int x = 0; x < DEV_W; ++x) {
103                SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
104                *pixel = get_src_color(x, y);
105            }
106        }
107    }
108    return bmp;
109}
110
111static void fill_src_canvas(SkCanvas* canvas) {
112    canvas->save();
113    canvas->setMatrix(SkMatrix::I());
114    canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
115    SkPaint paint;
116    paint.setXfermodeMode(SkXfermode::kSrc_Mode);
117    canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
118    canvas->restore();
119}
120
121#if SK_SUPPORT_GPU
122static void fill_src_texture(GrTexture* texture) {
123    SkBitmap bmp = make_src_bitmap();
124    bmp.lockPixels();
125    texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
126                         bmp.rowBytes());
127    bmp.unlockPixels();
128}
129#endif
130
131static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
132    SkASSERT(bitmap->lockPixelsAreWritable());
133    SkAutoLockPixels alp(*bitmap);
134    int w = bitmap->width();
135    int h = bitmap->height();
136    intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
137    for (int y = 0; y < h; ++y) {
138        for (int x = 0; x < w; ++x) {
139            SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
140            *pixel = get_dst_bmp_init_color(x, y, w);
141        }
142    }
143}
144
145static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
146    if (!didPremulConversion) {
147        return a == b;
148    }
149    int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
150    int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
151    int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
152    int32_t aB = SkGetPackedB32(a);
153
154    int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
155    int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
156    int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
157    int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
158
159    return aA == bA &&
160           SkAbs32(aR - bR) <= 1 &&
161           SkAbs32(aG - bG) <= 1 &&
162           SkAbs32(aB - bB) <= 1;
163}
164
165// checks the bitmap contains correct pixels after the readPixels
166// if the bitmap was prefilled with pixels it checks that these weren't
167// overwritten in the area outside the readPixels.
168static bool check_read(skiatest::Reporter* reporter,
169                       const SkBitmap& bitmap,
170                       int x, int y,
171                       bool checkCanvasPixels,
172                       bool checkBitmapPixels) {
173    SkASSERT(4 == bitmap.bytesPerPixel());
174    SkASSERT(!bitmap.isNull());
175    SkASSERT(checkCanvasPixels || checkBitmapPixels);
176
177    const SkColorType ct = bitmap.colorType();
178    const SkAlphaType at = bitmap.alphaType();
179
180    int bw = bitmap.width();
181    int bh = bitmap.height();
182
183    SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
184    SkIRect clippedSrcRect = DEV_RECT;
185    if (!clippedSrcRect.intersect(srcRect)) {
186        clippedSrcRect.setEmpty();
187    }
188    SkAutoLockPixels alp(bitmap);
189    for (int by = 0; by < bh; ++by) {
190        for (int bx = 0; bx < bw; ++bx) {
191            int devx = bx + srcRect.fLeft;
192            int devy = by + srcRect.fTop;
193
194            const uint32_t* pixel = bitmap.getAddr32(bx, by);
195
196            if (clippedSrcRect.contains(devx, devy)) {
197                if (checkCanvasPixels) {
198                    SkPMColor canvasPixel = get_src_color(devx, devy);
199                    bool didPremul;
200                    SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
201                    if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
202                        ERRORF(reporter, "Expected readback pixel value 0x%08x, got 0x%08x. "
203                               "Readback was unpremul: %d", canvasPixel, pmPixel, didPremul);
204                        return false;
205                    }
206                }
207            } else if (checkBitmapPixels) {
208                uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
209                if (origDstPixel != *pixel) {
210                    ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
211                           "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
212                    return false;
213                }
214            }
215        }
216    }
217    return true;
218}
219
220enum BitmapInit {
221    kFirstBitmapInit = 0,
222
223    kNoPixels_BitmapInit = kFirstBitmapInit,
224    kTight_BitmapInit,
225    kRowBytes_BitmapInit,
226
227    kBitmapInitCnt
228};
229
230static BitmapInit nextBMI(BitmapInit bmi) {
231    int x = bmi;
232    return static_cast<BitmapInit>(++x);
233}
234
235static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
236                        SkAlphaType at) {
237    SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
238    size_t rowBytes = 0;
239    bool alloc = true;
240    switch (init) {
241        case kNoPixels_BitmapInit:
242            alloc = false;
243        case kTight_BitmapInit:
244            break;
245        case kRowBytes_BitmapInit:
246            rowBytes = (info.width() + 16) * sizeof(SkPMColor);
247            break;
248        default:
249            SkASSERT(0);
250            break;
251    }
252
253    if (alloc) {
254        bitmap->allocPixels(info);
255    } else {
256        bitmap->setInfo(info, rowBytes);
257    }
258}
259
260DEF_GPUTEST(ReadPixels, reporter, factory) {
261    const SkIRect testRects[] = {
262        // entire thing
263        DEV_RECT,
264        // larger on all sides
265        SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
266        // fully contained
267        SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
268        // outside top left
269        SkIRect::MakeLTRB(-10, -10, -1, -1),
270        // touching top left corner
271        SkIRect::MakeLTRB(-10, -10, 0, 0),
272        // overlapping top left corner
273        SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
274        // overlapping top left and top right corners
275        SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, DEV_H / 4),
276        // touching entire top edge
277        SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, 0),
278        // overlapping top right corner
279        SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W  + 10, DEV_H / 4),
280        // contained in x, overlapping top edge
281        SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W  / 4, DEV_H / 4),
282        // outside top right corner
283        SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
284        // touching top right corner
285        SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
286        // overlapping top left and bottom left corners
287        SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
288        // touching entire left edge
289        SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
290        // overlapping bottom left corner
291        SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
292        // contained in y, overlapping left edge
293        SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
294        // outside bottom left corner
295        SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
296        // touching bottom left corner
297        SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
298        // overlapping bottom left and bottom right corners
299        SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
300        // touching entire left edge
301        SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
302        // overlapping bottom right corner
303        SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
304        // overlapping top right and bottom right corners
305        SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
306    };
307
308    for (int dtype = 0; dtype < 3; ++dtype) {
309        int glCtxTypeCnt = 1;
310#if SK_SUPPORT_GPU
311        // On the GPU we will also try reading back from a non-renderable texture.
312        SkAutoTUnref<GrTexture> texture;
313
314        if (0 != dtype)  {
315            glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
316        }
317#endif
318        const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
319        for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
320            SkAutoTUnref<SkSurface> surface;
321            if (0 == dtype) {
322                surface.reset(SkSurface::NewRaster(info));
323            } else {
324#if SK_SUPPORT_GPU
325                GrContextFactory::GLContextType type =
326                    static_cast<GrContextFactory::GLContextType>(glCtxType);
327                if (!GrContextFactory::IsRenderingGLContext(type)) {
328                    continue;
329                }
330                GrContext* context = factory->get(type);
331                if (nullptr == context) {
332                    continue;
333                }
334                GrSurfaceDesc desc;
335                desc.fFlags = kRenderTarget_GrSurfaceFlag;
336                desc.fWidth = DEV_W;
337                desc.fHeight = DEV_H;
338                desc.fConfig = kSkia8888_GrPixelConfig;
339                desc.fOrigin = 1 == dtype ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
340                SkAutoTUnref<GrTexture> surfaceTexture(
341                    context->textureProvider()->createTexture(desc, false));
342                surface.reset(SkSurface::NewRenderTargetDirect(surfaceTexture->asRenderTarget()));
343                desc.fFlags = kNone_GrSurfaceFlags;
344
345                texture.reset(context->textureProvider()->createTexture(desc, false));
346#else
347                continue;
348#endif
349            }
350            SkCanvas& canvas = *surface->getCanvas();
351            fill_src_canvas(&canvas);
352
353#if SK_SUPPORT_GPU
354            if (texture) {
355                fill_src_texture(texture);
356            }
357#endif
358
359            static const struct {
360                SkColorType fColorType;
361                SkAlphaType fAlphaType;
362            } gReadConfigs[] = {
363                { kRGBA_8888_SkColorType,   kPremul_SkAlphaType },
364                { kRGBA_8888_SkColorType,   kUnpremul_SkAlphaType },
365                { kBGRA_8888_SkColorType,   kPremul_SkAlphaType },
366                { kBGRA_8888_SkColorType,   kUnpremul_SkAlphaType },
367            };
368            for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
369                const SkIRect& srcRect = testRects[rect];
370                for (BitmapInit bmi = kFirstBitmapInit; bmi < kBitmapInitCnt; bmi = nextBMI(bmi)) {
371                    for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
372                        SkBitmap bmp;
373                        init_bitmap(&bmp, srcRect, bmi,
374                                    gReadConfigs[c].fColorType, gReadConfigs[c].fAlphaType);
375
376                        // if the bitmap has pixels allocated before the readPixels,
377                        // note that and fill them with pattern
378                        bool startsWithPixels = !bmp.isNull();
379                        if (startsWithPixels) {
380                            fill_dst_bmp_with_init_data(&bmp);
381                        }
382                        uint32_t idBefore = surface->generationID();
383                        bool success = canvas.readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
384                        uint32_t idAfter = surface->generationID();
385
386                        // we expect to succeed when the read isn't fully clipped
387                        // out.
388                        bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
389                        // determine whether we expected the read to succeed.
390                        REPORTER_ASSERT(reporter, success == expectSuccess);
391                        // read pixels should never change the gen id
392                        REPORTER_ASSERT(reporter, idBefore == idAfter);
393
394                        if (success || startsWithPixels) {
395                            check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
396                                       success, startsWithPixels);
397                        } else {
398                            // if we had no pixels beforehand and the readPixels
399                            // failed then our bitmap should still not have pixels
400                            REPORTER_ASSERT(reporter, bmp.isNull());
401                        }
402#if SK_SUPPORT_GPU
403                        // Try doing the read directly from a non-renderable texture
404                        if (texture && startsWithPixels) {
405                            fill_dst_bmp_with_init_data(&bmp);
406                            GrPixelConfig dstConfig =
407                                SkImageInfo2GrPixelConfig(gReadConfigs[c].fColorType,
408                                                          gReadConfigs[c].fAlphaType,
409                                                          kLinear_SkColorProfileType);
410                            uint32_t flags = 0;
411                            if (gReadConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
412                                flags = GrContext::kUnpremul_PixelOpsFlag;
413                            }
414                            bmp.lockPixels();
415                            success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
416                                                bmp.height(), dstConfig, bmp.getPixels(),
417                                                bmp.rowBytes(), flags);
418                            bmp.unlockPixels();
419                            check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
420                                       success, true);
421                        }
422#endif
423                    }
424                    // check the old webkit version of readPixels that clips the
425                    // bitmap size
426                    SkBitmap wkbmp;
427                    bool success = canvas.readPixels(srcRect, &wkbmp);
428                    SkIRect clippedRect = DEV_RECT;
429                    if (clippedRect.intersect(srcRect)) {
430                        REPORTER_ASSERT(reporter, success);
431                        REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
432                        REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
433                        check_read(reporter, wkbmp, clippedRect.fLeft,
434                                   clippedRect.fTop, true, false);
435                    } else {
436                        REPORTER_ASSERT(reporter, !success);
437                    }
438                }
439            }
440        }
441    }
442}
443
444/////////////////////
445#if SK_SUPPORT_GPU
446
447// make_ringed_bitmap was lifted from gm/bleed.cpp, as that GM was what showed the following
448// bug when a change was made to SkImage_Raster.cpp. It is possible that other test bitmaps
449// would also tickle https://bug.skia.org/4351 but this one is know to do it, so I've pasted the code
450// here so we have a dependable repro case.
451
452// Create a black&white checked texture with 2 1-pixel rings
453// around the outside edge. The inner ring is red and the outer ring is blue.
454static void make_ringed_bitmap(SkBitmap* result, int width, int height) {
455    SkASSERT(0 == width % 2 && 0 == height % 2);
456
457    static const SkPMColor kRed = SkPreMultiplyColor(SK_ColorRED);
458    static const SkPMColor kBlue = SkPreMultiplyColor(SK_ColorBLUE);
459    static const SkPMColor kBlack = SkPreMultiplyColor(SK_ColorBLACK);
460    static const SkPMColor kWhite = SkPreMultiplyColor(SK_ColorWHITE);
461
462    result->allocN32Pixels(width, height, true);
463
464    SkPMColor* scanline = result->getAddr32(0, 0);
465    for (int x = 0; x < width; ++x) {
466        scanline[x] = kBlue;
467    }
468    scanline = result->getAddr32(0, 1);
469    scanline[0] = kBlue;
470    for (int x = 1; x < width - 1; ++x) {
471        scanline[x] = kRed;
472    }
473    scanline[width-1] = kBlue;
474
475    for (int y = 2; y < height/2; ++y) {
476        scanline = result->getAddr32(0, y);
477        scanline[0] = kBlue;
478        scanline[1] = kRed;
479        for (int x = 2; x < width/2; ++x) {
480            scanline[x] = kBlack;
481        }
482        for (int x = width/2; x < width-2; ++x) {
483            scanline[x] = kWhite;
484        }
485        scanline[width-2] = kRed;
486        scanline[width-1] = kBlue;
487    }
488
489    for (int y = height/2; y < height-2; ++y) {
490        scanline = result->getAddr32(0, y);
491        scanline[0] = kBlue;
492        scanline[1] = kRed;
493        for (int x = 2; x < width/2; ++x) {
494            scanline[x] = kWhite;
495        }
496        for (int x = width/2; x < width-2; ++x) {
497            scanline[x] = kBlack;
498        }
499        scanline[width-2] = kRed;
500        scanline[width-1] = kBlue;
501    }
502
503    scanline = result->getAddr32(0, height-2);
504    scanline[0] = kBlue;
505    for (int x = 1; x < width - 1; ++x) {
506        scanline[x] = kRed;
507    }
508    scanline[width-1] = kBlue;
509
510    scanline = result->getAddr32(0, height-1);
511    for (int x = 0; x < width; ++x) {
512        scanline[x] = kBlue;
513    }
514    result->setImmutable();
515}
516
517static void compare_textures(skiatest::Reporter* reporter, GrTexture* txa, GrTexture* txb) {
518    REPORTER_ASSERT(reporter, txa->width() == 2);
519    REPORTER_ASSERT(reporter, txa->height() == 2);
520    REPORTER_ASSERT(reporter, txb->width() == 2);
521    REPORTER_ASSERT(reporter, txb->height() == 2);
522    REPORTER_ASSERT(reporter, txa->config() == txb->config());
523
524    SkPMColor pixelsA[4], pixelsB[4];
525    REPORTER_ASSERT(reporter, txa->readPixels(0, 0, 2, 2, txa->config(), pixelsA));
526    REPORTER_ASSERT(reporter, txb->readPixels(0, 0, 2, 2, txa->config(), pixelsB));
527    REPORTER_ASSERT(reporter, 0 == memcmp(pixelsA, pixelsB, sizeof(pixelsA)));
528}
529
530static SkData* draw_into_surface(SkSurface* surf, const SkBitmap& bm, SkFilterQuality quality) {
531    SkCanvas* canvas = surf->getCanvas();
532    canvas->clear(SK_ColorBLUE);
533
534    SkPaint paint;
535    paint.setFilterQuality(quality);
536
537    canvas->translate(40, 100);
538    canvas->rotate(30);
539    canvas->scale(20, 30);
540    canvas->translate(-SkScalarHalf(bm.width()), -SkScalarHalf(bm.height()));
541    canvas->drawBitmap(bm, 0, 0, &paint);
542
543    SkAutoTUnref<SkImage> image(surf->newImageSnapshot());
544    return image->encode();
545}
546
547#include "SkStream.h"
548static void dump_to_file(const char name[], SkData* data) {
549    SkFILEWStream file(name);
550    file.write(data->data(), data->size());
551}
552
553/*
554 *  Test two different ways to turn a subset of a bitmap into a texture
555 *  - subset and then upload to a texture
556 *  - upload to a texture and then subset
557 *
558 *  These two techniques result in the same pixels (ala readPixels)
559 *  but when we draw them (rotated+scaled) we don't always get the same results.
560 *
561 *  https://bug.skia.org/4351
562 */
563DEF_GPUTEST(ReadPixels_Subset_Gpu, reporter, factory) {
564    GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType);
565    if (!ctx) {
566        REPORTER_ASSERT(reporter, false);
567        return;
568    }
569
570    SkBitmap bitmap;
571    make_ringed_bitmap(&bitmap, 6, 6);
572    const SkIRect subset = SkIRect::MakeLTRB(2, 2, 4, 4);
573
574    // make two textures...
575    SkBitmap bm_subset, tx_subset;
576
577    // ... one from a texture-subset
578    SkAutoTUnref<GrTexture> fullTx(GrRefCachedBitmapTexture(ctx, bitmap,
579                                                            GrTextureParams::ClampNoFilter()));
580    SkBitmap tx_full;
581    GrWrapTextureInBitmap(fullTx, bitmap.width(), bitmap.height(), true, &tx_full);
582    tx_full.extractSubset(&tx_subset, subset);
583
584    // ... one from a bitmap-subset
585    SkBitmap tmp_subset;
586    bitmap.extractSubset(&tmp_subset, subset);
587    SkAutoTUnref<GrTexture> subsetTx(GrRefCachedBitmapTexture(ctx, tmp_subset,
588                                                              GrTextureParams::ClampNoFilter()));
589    GrWrapTextureInBitmap(subsetTx, tmp_subset.width(), tmp_subset.height(), true, &bm_subset);
590
591    // did we get the same subset?
592    compare_textures(reporter, bm_subset.getTexture(), tx_subset.getTexture());
593
594    // do they draw the same?
595    const SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
596    SkAutoTUnref<SkSurface> surfA(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0));
597    SkAutoTUnref<SkSurface> surfB(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0));
598
599    if (false) {
600        //
601        //  BUG: depending on the driver, if we calls this with various quality settings, it
602        //       may fail.
603        //
604        SkFilterQuality quality = kLow_SkFilterQuality;
605
606        SkAutoTUnref<SkData> dataA(draw_into_surface(surfA, bm_subset, quality));
607        SkAutoTUnref<SkData> dataB(draw_into_surface(surfB, tx_subset, quality));
608
609        REPORTER_ASSERT(reporter, dataA->equals(dataB));
610        if (false) {
611            dump_to_file("test_image_A.png", dataA);
612            dump_to_file("test_image_B.png", dataB);
613        }
614    }
615}
616#endif
617