PixelDumpSupportBlackBerry.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2011, 2012 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "PixelDumpSupportBlackBerry.h"
  20. #include "BackingStore.h"
  21. #include "BlackBerryPlatformExecutableMessage.h"
  22. #include "BlackBerryPlatformGraphics.h"
  23. #include "BlackBerryPlatformGraphicsContext.h"
  24. #include "BlackBerryPlatformGraphicsImpl.h"
  25. #include "BlackBerryPlatformMessageClient.h"
  26. #include "DumpRenderTreeBlackBerry.h"
  27. #include "PNGImageEncoder.h"
  28. #include "PixelDumpSupport.h"
  29. #include "WebPage.h"
  30. #include "WebPageClient.h"
  31. #include <BlackBerryPlatformWindow.h>
  32. #include <wtf/MD5.h>
  33. #include <wtf/Vector.h>
  34. using namespace BlackBerry::WebKit;
  35. using namespace BlackBerry;
  36. using namespace WTF;
  37. void readPixelsUserInterfaceThread(BlackBerry::Platform::Graphics::PlatformGraphicsContext* context, const BlackBerry::Platform::IntRect& srcRect, unsigned char* pixels)
  38. {
  39. context->readPixels(srcRect, pixels, false, false);
  40. }
  41. PassRefPtr<BitmapContext> createBitmapContextFromWebView(bool /*onscreen*/, bool /*incrementalRepaint*/, bool /*sweepHorizontally*/, bool /*drawSelectionRect*/)
  42. {
  43. Platform::Graphics::Window* window = DumpRenderTree::currentInstance()->page()->client()->window();
  44. ASSERT(window);
  45. // The BackingStore has a queue of pending jobs, which are run on idle
  46. // and which may not have been run yet.
  47. BackingStore* backingStore = DumpRenderTree::currentInstance()->page()->backingStore();
  48. while (backingStore->hasBlitJobs())
  49. backingStore->blitOnIdle();
  50. const Platform::IntRect& windowRect = window->viewportRect();
  51. const Platform::IntSize& windowSize = window->viewportSize();
  52. unsigned char* data = new unsigned char[windowSize.width() * windowSize.height() * 4];
  53. BlackBerry::Platform::Graphics::Buffer* buffer = BlackBerry::Platform::Graphics::createBuffer(windowSize, BlackBerry::Platform::Graphics::AlwaysBacked);
  54. BlackBerry::Platform::Graphics::Drawable* drawable = BlackBerry::Platform::Graphics::lockBufferDrawable(buffer);
  55. if (drawable) {
  56. backingStore->drawContents(drawable, windowRect, windowSize);
  57. BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
  58. BlackBerry::Platform::createFunctionCallMessage(&readPixelsUserInterfaceThread, drawable, windowRect, data));
  59. BlackBerry::Platform::Graphics::releaseBufferDrawable(buffer);
  60. }
  61. BlackBerry::Platform::Graphics::destroyBuffer(buffer);
  62. return BitmapContext::createByAdoptingData(data, windowSize.width(), windowSize.height());
  63. }
  64. void computeMD5HashStringForBitmapContext(BitmapContext* context, char hashString[33])
  65. {
  66. int pixelsWide = context->m_width;
  67. int pixelsHigh = context->m_height;
  68. int bytesPerRow = context->m_width * 4;
  69. unsigned char* pixelData = context->m_data;
  70. MD5 md5;
  71. for (int i = 0; i < pixelsHigh; ++i) {
  72. md5.addBytes(pixelData, 4 * pixelsWide);
  73. pixelData += bytesPerRow;
  74. }
  75. Vector<uint8_t, 16> hash;
  76. md5.checksum(hash);
  77. hashString[0] = '\0';
  78. for (int i = 0; i < 16; ++i)
  79. snprintf(hashString, 33, "%s%02x", hashString, hash[i]);
  80. }
  81. static void printPNG(BitmapContext* context, const char* checksum)
  82. {
  83. Vector<unsigned char> pngData;
  84. encodeBitmapToPNG(context->m_data, context->m_width, context->m_height, &pngData);
  85. printPNG(pngData.data(), pngData.size(), checksum);
  86. }
  87. void dumpBitmap(BitmapContext* context, const char* checksum)
  88. {
  89. printPNG(context, checksum);
  90. }