16451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips/*
26451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips * Copyright 2016 Google Inc.
36451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips *
46451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips * Use of this source code is governed by a BSD-style license that can be
56451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips * found in the LICENSE file.
66451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips */
76451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
8986480a71f4e860663ced7ad90a1fe346a164afbMike Reed#include "SkBitmapDevice.h"
9986480a71f4e860663ced7ad90a1fe346a164afbMike Reed#include "SkDevice.h"
106451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#include "SkSpecialImage.h"
116451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
126451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#if SK_SUPPORT_GPU
136451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#include "SkGpuDevice.h"
146451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#endif
156451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
166451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#include "Test.h"
176451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
186451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillipsclass DeviceTestingAccess {
196451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillipspublic:
206451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static sk_sp<SkSpecialImage> MakeSpecial(SkBaseDevice* dev, const SkBitmap& bm) {
216451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips        return dev->makeSpecial(bm);
226451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    }
236451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
246451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static sk_sp<SkSpecialImage> MakeSpecial(SkBaseDevice* dev, SkImage* img) {
256451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips        return dev->makeSpecial(img);
266451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    }
276451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
286451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static sk_sp<SkSpecialImage> SnapSpecial(SkBaseDevice* dev) {
296451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips        return dev->snapSpecial();
306451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    }
316451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips};
326451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
336451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips// TODO: re-enable this when Raster methods are implemented
346451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#if 0
356451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillipsDEF_TEST(SpecialImage_BitmapDevice, reporter) {
366451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static const int kWidth = 100;
376451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static const int kHeight = 90;
386451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
396451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight);
406451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
41342b7acc46550af5fbefc6f9313231ede11ed692Hal Canary    sk_sp<SkBaseDevice> bmDev(SkBitmapDevice::Create(ii));
426451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
436451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkBitmap bm;
446451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    bm.tryAllocN32Pixels(kWidth, kHeight);
456451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
466451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Create a raster-backed special image from a raster-backed SkBitmap
476451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(bmDev.get(), bm);
486451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(!special->isTextureBacked());
496451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kWidth == special->width());
506451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kHeight == special->height());
516451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(bm.getGenerationID() == special->uniqueID());
526451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
536451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
546451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Create a raster-backed special image from a raster-backed SkImage
556451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
566451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    special = DeviceTestingAccess::MakeSpecial(bmDev.get(), image.get());
576451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(!special->isTextureBacked());
586451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kWidth == special->width());
596451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kHeight == special->height());
606451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(bm.getGenerationID() == special->uniqueID());
616451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
626451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
636451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Snap the device as a raster-backed special image
646451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    special = DeviceTestingAccess::SnapSpecial(bmDev.get());
656451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(!special->isTextureBacked());
666451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(2*kWidth == special->width());
676451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(2*kHeight == special->height());
686451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset());
696451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips}
706451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#endif
716451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
726451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
736451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#if SK_SUPPORT_GPU
746451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
756451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillipsDEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_GPUDevice, reporter, ctxInfo) {
766451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    GrContext* context = ctxInfo.grContext();
776451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
786451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static const int kWidth = 100;
796451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    static const int kHeight = 90;
806451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
816451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight);
826451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
836451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    sk_sp<SkBaseDevice> gpuDev(SkGpuDevice::Make(context, SkBudgeted::kNo, ii,
84bdecacfbe47bc7211336bb847bb33c00ef85ea3eBrian Salomon                                                 1, kBottomLeft_GrSurfaceOrigin, nullptr,
85e252f08982b0c747cd4d34c00ce413ab1005e99aGreg Daniel                                                 GrMipMapped::kNo,
867e922765545f42ce691e4f3d5fbbd4e44ba47ff1robertphillips                                                 SkGpuDevice::kClear_InitContents));
876451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
886451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkBitmap bm;
896451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkAssertResult(bm.tryAllocN32Pixels(kWidth, kHeight));
906451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
916451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Create a gpu-backed special image from a raster-backed SkBitmap
926451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(gpuDev.get(), bm);
936451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(special->isTextureBacked());
946451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kWidth == special->width());
956451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kHeight == special->height());
966451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(bm.getGenerationID() == special->uniqueID());
976451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
986451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
996451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Create a gpu-backed special image from a raster-backed SkImage
1006451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
1016451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    special = DeviceTestingAccess::MakeSpecial(gpuDev.get(), image.get());
1026451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(special->isTextureBacked());
1036451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kWidth == special->width());
1046451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kHeight == special->height());
1056451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // TODO: Hmmm, this is a bit unexpected
1066451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(image->uniqueID() != special->uniqueID());
1076451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
1086451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
1096451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Create a gpu-backed special image from a gpu-backed SkImage
110041f7dfc577822f1e97721a865f86a9a02660cf9Brian Osman    SkColorSpace* legacyColorSpace = nullptr;
111041f7dfc577822f1e97721a865f86a9a02660cf9Brian Osman    image = image->makeTextureImage(context, legacyColorSpace);
1126451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    special = DeviceTestingAccess::MakeSpecial(gpuDev.get(), image.get());
1136451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(special->isTextureBacked());
1146451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kWidth == special->width());
1156451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(kHeight == special->height());
1166451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(image->uniqueID() == special->uniqueID());
1176451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
1186451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
1196451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    // Snap the device as a gpu-backed special image
1206451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    special = DeviceTestingAccess::SnapSpecial(gpuDev.get());
1216451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(special->isTextureBacked());
1226451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(2*kWidth == special->width());
1236451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(2*kHeight == special->height());
1246451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips    SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset());
1256451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips}
1266451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips
1276451a0cea6007aff54565ec82e2f86cb1d32ecc7robertphillips#endif
128