textblobuseaftergpufree.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkRect.h"
  12. #include "include/core/SkScalar.h"
  13. #include "include/core/SkSize.h"
  14. #include "include/core/SkString.h"
  15. #include "include/core/SkTextBlob.h"
  16. #include "include/core/SkTypeface.h"
  17. #include "include/gpu/GrContext.h"
  18. #include "tools/ToolUtils.h"
  19. #include <string.h>
  20. class GrRenderTargetContext;
  21. // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
  22. namespace skiagm {
  23. class TextBlobUseAfterGpuFree : public GpuGM {
  24. public:
  25. TextBlobUseAfterGpuFree() { }
  26. protected:
  27. SkString onShortName() override {
  28. return SkString("textblobuseaftergpufree");
  29. }
  30. SkISize onISize() override {
  31. return SkISize::Make(kWidth, kHeight);
  32. }
  33. void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
  34. const char text[] = "Hamburgefons";
  35. SkFont font(ToolUtils::create_portable_typeface(), 20);
  36. auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
  37. // draw textblob
  38. SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f);
  39. SkPaint rectPaint;
  40. rectPaint.setColor(0xffffffff);
  41. canvas->drawRect(rect, rectPaint);
  42. canvas->drawTextBlob(blob, 20, 60, SkPaint());
  43. // This text should look fine
  44. context->freeGpuResources();
  45. canvas->drawTextBlob(blob, 20, 160, SkPaint());
  46. }
  47. private:
  48. static constexpr int kWidth = 200;
  49. static constexpr int kHeight = 200;
  50. typedef GM INHERITED;
  51. };
  52. //////////////////////////////////////////////////////////////////////////////
  53. DEF_GM(return new TextBlobUseAfterGpuFree;)
  54. }